//
//  BeginTestViewController.m
//  IpadTestB
//
//  Created by edianzu on 2017/7/6.
//  Copyright © 2017年 ruilai. All rights reserved.
//

#import "BeginTestViewController.h"
#import "ProjectCell.h"
#import "ChooseWayCell.h"
#import "TestingCell.h"
#import "InputDataView.h"
#import "AddChildView.h"
#import "BaseAlertView.h"
@interface BeginTestViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate,SDDatePickerViewProtocol>
@property(nonatomic,strong)InputDataView * inputDataView;
@property(nonatomic,strong)ChildModel * rightNowModel;
@property(nonatomic,assign)NSInteger indexpathrow;
@property(nonatomic,strong)AddChildView * AddChildView;
@property(nonatomic,strong)BaseAlertView * baseAlertView;
@property(nonatomic,strong)NSMutableDictionary * allDataDictionary;

@end

@implementation BeginTestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(change:) name:UITextFieldTextDidChangeNotification object:self.inputDataView.inputTextField];//此处监听用于 输入数据时，有数据不让按钮可以点击
    //添加键盘监听事件
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShoww:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillHidee:) name:UIKeyboardWillHideNotification object:nil];
    // Do any additional setup after loading the view.
    self.title = @"测试";
    self.view.backgroundColor = UIColorFromRGB(241, 246, 248);
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont boldSystemFontOfSize:23*AutoScaleHorizontal],
       NSForegroundColorAttributeName:[UIColor whiteColor]}];
    ////////////////左按钮
    UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    leftBtn.frame = CGRectMake(0, 0, 35,35);
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"navBack"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(leftBarBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * leftBarBtn = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];;
    //创建UIBarButtonSystemItemFixedSpace
    UIBarButtonItem * spaceItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    //将宽度设为负值
    spaceItem.width = 15;
    //将两个BarButtonItem都返回给NavigationItem
    self.navigationItem.leftBarButtonItems = @[spaceItem,leftBarBtn];
    
    //创建右边按钮
    UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(0, 0, 140,35);
    //    rightBtn.layer.cornerRadius =10;
    rightBtn.backgroundColor = [UIColor clearColor];
    //    [rightBtn setImage:[UIImage imageNamed:@"navRightBtn"] forState:UIControlStateNormal];
    [rightBtn setTitle:@"上传" forState:UIControlStateNormal];
    [rightBtn setImage:[UIImage imageNamed:@"upload"] forState:UIControlStateNormal];
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:20*AutoScaleHorizontal];
    [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(rightBarBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * rightBarBtn = [[UIBarButtonItem alloc]initWithCustomView:rightBtn];;
    
    
    UIButton * rightBtn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn2.frame = CGRectMake(-145, 0, 140,35);
    rightBtn2.layer.cornerRadius =10;
    rightBtn2.backgroundColor = [UIColor whiteColor];
    //    [rightBtn setImage:[UIImage imageNamed:@"navRightBtn"] forState:UIControlStateNormal];
    [rightBtn2 setTitle:@"十  添加孩子" forState:UIControlStateNormal];
    rightBtn2.titleLabel.font = [UIFont systemFontOfSize:20*AutoScaleHorizontal];
    [rightBtn2 setTitleColor:UIColorFromRGB(26, 167, 157) forState:UIControlStateNormal];
    [rightBtn2 addTarget:self action:@selector(rightBarBtnClicked2) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * rightBarBtn2 = [[UIBarButtonItem alloc]initWithCustomView:rightBtn2];;
    
    //创建UIBarButtonSystemItemFixedSpace
    UIBarButtonItem * spaceItemm = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    //将宽度设为负值
    spaceItemm.width = -5;
    //将两个BarButtonItem都返回给NavigationItem
    self.navigationItem.rightBarButtonItems = @[spaceItemm,rightBarBtn,rightBarBtn2];
    self.rightNowProjectId = @1;
    [self getData];
    [self creatUI];
    
}

-(void)getData{
    NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
    //初始选中身高
    
    self.projectArray = [[NSMutableArray alloc]initWithCapacity:0];
    self.resultArray1 = [[NSMutableArray alloc]initWithCapacity:0];
    self.resultArray2 = [[NSMutableArray alloc]initWithCapacity:0];
    self.resultArray4 = [[NSMutableArray alloc]initWithCapacity:0];
    self.resultArray5 = [[NSMutableArray alloc]initWithCapacity:0];
    self.resultArray6 = [[NSMutableArray alloc]initWithCapacity:0];
    self.resultArray7 = [[NSMutableArray alloc]initWithCapacity:0];
    self.resultArray8 = [[NSMutableArray alloc]initWithCapacity:0];
    self.resultArray9 = [[NSMutableArray alloc]initWithCapacity:0];
    self.resultArray10 = [[NSMutableArray alloc]initWithCapacity:0];
    
//    self.resultDetailArray = [[NSMutableArray alloc]initWithCapacity:0];
    DetailListModel * model1 = [[DetailListModel alloc]init];
    model1.Name = @"身高";
    model1.ProjectId = @1;
    model1.isSelected = @"1";//默认选择第一项
    [self.projectArray addObject:model1];
    
    //当前默认第一项
    
    
    DetailListModel * model2 = [[DetailListModel alloc]init];
    model2.Name = @"体重";
    model2.ProjectId = @2;
    model2.isSelected = @"0";
    [self.projectArray addObject:model2];
    
    DetailListModel * model3 = [[DetailListModel alloc]init];
    model3.Name = @"15米节奏往返跑";
    model3.ProjectId = @4;
    model3.isSelected = @"0";
    [self.projectArray addObject:model3];
    
    DetailListModel * model4 = [[DetailListModel alloc]init];
    model4.Name = @"仰卧卷腹";
    model4.ProjectId = @5;
    model4.isSelected = @"0";
    [self.projectArray addObject:model4];
    
    DetailListModel * model5 = [[DetailListModel alloc]init];
    model5.Name = @"俯卧背伸";
    model5.ProjectId = @6;
    model5.isSelected = @"0";
    [self.projectArray addObject:model5];
    
    DetailListModel * model6 = [[DetailListModel alloc]init];
    model6.Name = @"90°俯卧撑";
    model6.ProjectId = @7;
    model6.isSelected = @"0";
    [self.projectArray addObject:model6];
    
    DetailListModel * model7 = [[DetailListModel alloc]init];
    model7.Name = @"坐位体前屈";
    model7.ProjectId = @8;
    model7.isSelected = @"0";
    [self.projectArray addObject:model7];
    
    DetailListModel * model8 = [[DetailListModel alloc]init];
    model8.Name = @"立定跳远";
    model8.ProjectId = @9;
    model8.isSelected = @"0";
    [self.projectArray addObject:model8];
    
    DetailListModel * model9 = [[DetailListModel alloc]init];
    model9.Name = @"走平衡木";
    model9.ProjectId = @10;
    model9.isSelected = @"0";
    [self.projectArray addObject:model9];
    
    
    
       
    //获取全部数据
    NSString* str = [user objectForKey:@"AllData"];
    NSData *AlljsonData = [str dataUsingEncoding:NSUTF8StringEncoding];
    _allDataDictionary = [NSJSONSerialization JSONObjectWithData:AlljsonData options:NSJSONReadingMutableContainers error:nil];
    NSMutableArray * testArray = [NSMutableArray arrayWithArray: [_allDataDictionary objectForKey:@"TestResult"]];//有结果的数据
    //本次营销计划选中的人(通过MASTER ID  判断)
    NSString * FS = [user objectForKey:@"finalSaleDic"];//本地保存的本次选中的营销人
    if (FS !=nil) {
    
    NSData * jsonData = [FS dataUsingEncoding:NSUTF8StringEncoding];
    NSMutableDictionary * mdic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
    NSMutableArray * masterIdArray = [mdic objectForKey:@"finalSaleDic"];
    
    for (NSNumber * iidd in masterIdArray) {//循环遍历已选中的孩子ID
        
    for (NSMutableDictionary * dic in testArray) {//循环遍历testResult
        NSMutableDictionary * masterdic = [dic objectForKey:@"master"];
        NSMutableArray * detailArr = [dic objectForKey:@"detailList"];
        
        if ([[masterdic objectForKey:@"Id"]isEqual:iidd]) {//查找选中孩子的MASTER和RESULT
//            [self.resultArray addObject:dic];//将整个孩子的RESULT都加入数据源
            int a = 1;
            for (DetailListModel * mod in self.projectArray) {//总共九个项目，创建9组对象
                
            NSMutableDictionary * resultDic = [[NSMutableDictionary alloc]init];//对象字典（master.detailDic）
            [resultDic setObject:masterdic forKey:@"master"];
            for (NSMutableDictionary * dict in detailArr) {//遍历当前项目有数据的话DETAILDIC为数据。否则为空
                if ([[dict objectForKey:@"ProjectId"]isEqual:mod.ProjectId]) {
                    [resultDic setObject:dict forKey:@"detailDic"];
                }
                                                           }
                if ([resultDic objectForKey:@"detailDic"] ==nil) {//否则为空
                    NSMutableDictionary * newDetai =[[NSMutableDictionary alloc]init];
                    [resultDic setObject:newDetai forKey:@"detailDic"];
                }
                //分别按ID 将对象加入数组源
                if (a ==1) {
                    [self.resultArray1 addObject:resultDic];
                }
                if (a ==2) {
                    [self.resultArray2 addObject:resultDic];
                }
                if (a ==3) {
                    [self.resultArray4 addObject:resultDic];
                }
                if (a ==4) {
                    [self.resultArray5 addObject:resultDic];
                }
                if (a ==5) {
                    [self.resultArray6 addObject:resultDic];
                }
                if (a ==6) {
                    [self.resultArray7 addObject:resultDic];
                }
                if (a ==7) {
                    [self.resultArray8 addObject:resultDic];
                }
                if (a ==8) {
                    [self.resultArray9 addObject:resultDic];
                }
                if (a ==9) {
                    [self.resultArray10 addObject:resultDic];
                }
                a++;
                
                                                              }
        }
    }
    }
//    [user synchronize];
    }
    [self.rightTableView reloadData];
    
}

-(void)creatUI{
    //左侧TABLE
    self.leftTableView = [[UITableView alloc]init];
    self.leftTableView.backgroundColor =UIColorFromRGB(241, 244, 248);
    self.leftTableView.delegate =self;
    self.leftTableView.dataSource =self;
    self.leftTableView.showsVerticalScrollIndicator =NO;
    //无分割线
    self.leftTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //多余的CELL 隐藏
    self.leftTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.leftTableView registerClass:[ChooseWayCell class] forCellReuseIdentifier:@"ChooseWayCell"];
    [self.leftTableView registerClass:[ProjectCell class] forCellReuseIdentifier:@"ProjectCell"];
    [self.view addSubview:self.leftTableView];
    [self.leftTableView mas_makeConstraints:^(MASConstraintMaker * make) {
        make.left.mas_equalTo(self.view.mas_left).with.offset(75*AutoScaleHorizontal);
        make.top.mas_equalTo(self.view.mas_top).with.offset(20*AutoScaleVertical);
        make.size.mas_equalTo(CGSizeMake(240*AutoScaleHorizontal, (60*self.projectArray.count+80)*AutoScaleHorizontal));
    }];
    
    //测试与否选项按钮
    self.testAllBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.testAllBtn.selected = YES;
    [self.testAllBtn setBackgroundImage:[UIImage imageNamed:@"testUnS"] forState:UIControlStateNormal];
    [self.testAllBtn setBackgroundImage:[UIImage imageNamed:@"testS"] forState:UIControlStateSelected];
    [self.testAllBtn setTitle:@"全部" forState:UIControlStateNormal];
    NSInteger count =  self.resultArray1.count;
    [self.testAllBtn setTitle:[NSString stringWithFormat:@"全部  %d",count ] forState:UIControlStateNormal];

    [self.testAllBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [self.testAllBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.testAllBtn addTarget:self action:@selector(pressTestAll) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.testAllBtn];
    [self.testAllBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.leftTableView.mas_right).with.offset(30*AutoScaleHorizontal);
        make.top.mas_equalTo(self.view.mas_top).with.offset(30*AutoScaleVertical);
        make.size.mas_equalTo(CGSizeMake(250*AutoScaleHorizontal, 45 * AutoScaleVertical));
    }];
    
    self.UntestBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.UntestBtn.selected = NO;
    [self.UntestBtn setBackgroundImage:[UIImage imageNamed:@"untestUnS"] forState:UIControlStateNormal];
    [self.UntestBtn setBackgroundImage:[UIImage imageNamed:@"untestS"] forState:UIControlStateSelected];
//    [self.UntestBtn setTitle:@"未测" forState:UIControlStateNormal];
    ////////////////////////////////////////////////////////////////////////////////////
    NSInteger uninteger1 = 0;
    for (NSMutableDictionary * dict in self.resultArray1.reverseObjectEnumerator) {
        NSMutableDictionary * dic = [dict objectForKey:@"detailDic"];
        if ([dic objectForKey:@"ProjectId"]!=nil) {
            uninteger1 +=1;
        }
    }
    [self.UntestBtn setTitle:[NSString stringWithFormat:@"未测  %d",self.resultArray1.count-uninteger1] forState:UIControlStateNormal];
        ///////////////////////////////////////////////////////////////////////////
    [self.UntestBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [self.UntestBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.UntestBtn addTarget:self action:@selector(pressUnTest) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.UntestBtn];
    [self.UntestBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.testAllBtn.mas_right);
        make.top.mas_equalTo(self.view.mas_top).with.offset(30*AutoScaleVertical);
        make.size.mas_equalTo(CGSizeMake(250*AutoScaleHorizontal, 45 * AutoScaleVertical));
    }];
    
    //右侧TABLE
    self.rightTableView = [[UITableView alloc]init];
    self.rightTableView.backgroundColor =UIColorFromRGB(241, 244, 248);
    self.rightTableView.delegate =self;
    self.rightTableView.dataSource =self;
    self.rightTableView.showsVerticalScrollIndicator =NO;
    //无分割线
    self.rightTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //多余的CELL 隐藏
    self.rightTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.rightTableView registerClass:[TestingCell class] forCellReuseIdentifier:@"TestingCell"];
    [self.view addSubview:self.rightTableView];
    [self.rightTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.testAllBtn.mas_left);
        make.top.mas_equalTo(self.testAllBtn.mas_bottom).with.offset(25 * AutoScaleVertical);
        make.width.mas_equalTo(600*AutoScaleHorizontal);
        make.bottom.mas_equalTo(self.view.mas_bottom).with.offset(-25*AutoScaleVertical);
        
    }];
}

#pragma mark tableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.leftTableView) {
              if (indexPath.row ==0) {
        return 80*AutoScaleVertical;
              }else{
        return 60*AutoScaleVertical;
              }
    }else{
        return 85*AutoScaleVertical;
    }
    
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView ==self.leftTableView) {
        return self.projectArray.count + 1;
    }else{
        switch ([self.rightNowProjectId integerValue]) {
            case 1:
                return self.resultArray1.count;
                break;
            case 2:
                return self.resultArray2.count;
                break;
            case 4:
                return self.resultArray4.count;
                break;
            case 5:
                return self.resultArray5.count;
                break;
            case 6:
                return self.resultArray6.count;
                break;
            case 7:
                return self.resultArray7.count;
                break;
            case 8:
                return self.resultArray8.count;
                break;
            case 9:
                return self.resultArray9.count;
                break;
            case 10:
                return self.resultArray10.count;
                break;
            default:
                return 0;
                break;
        }
    }
    
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView ==self.leftTableView) {//左侧TABLEVIEW
        if (indexPath.row == 0) {//第一行测试
        ChooseWayCell * cell = [tableView dequeueReusableCellWithIdentifier:@"ChooseWayCell"];
        if (cell ==nil) {
            cell = [[ChooseWayCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ChooseWayCell"];
        }
        [cell relayoutPROJECTCell];
        return  cell;
        
        }else{//其他的行
        ProjectCell * cell = [tableView dequeueReusableCellWithIdentifier:@"ProjectCell"];
        if (cell ==nil) {
            cell = [[ProjectCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ProjectCell"];
        }
            [cell relayoutCellWithModel:self.projectArray[indexPath.row-1]];
        //CELL代码块方法
        __weak typeof(self) weak_self = self;
        cell.callChange = ^(NSString *select) {
            
            
            for (DetailListModel*mod in weak_self.projectArray) {
                mod.isSelected = @"0";
            }
            DetailListModel * mod = weak_self.projectArray[indexPath.row-1];
            //更换当前选中项目ID
            self.rightNowProjectId = mod.ProjectId;
            NSLog(@"%@",self.rightNowProjectId);
            mod.isSelected = @"1";
            [weak_self.projectArray replaceObjectAtIndex:indexPath.row-1 withObject:mod];
            //更改全部未测的数字
            if (weak_self.UntestBtn.selected ==YES) {
                [weak_self changeUntestWithNum:[self.rightNowProjectId integerValue]];
               [weak_self pressUnTest];
            
            }else{
                [weak_self changeTestNum:[self.rightNowProjectId integerValue]];
                [weak_self changeUntestWithNum:[self.rightNowProjectId integerValue]];
            
            }
            //如果是20米和平衡木 对数据源根据年龄排序
            
            if ([weak_self.rightNowProjectId isEqual:@4]) {
                
                for (int i = 0; i < weak_self.resultArray4.count; ++i) {
                    
                    //遍历数组的每一个`索引`（不包括最后一个,因为比较的是j+1）
                    for (int j = 0; j < weak_self.resultArray4.count-1; ++j) {
                        NSMutableDictionary * dict = weak_self.resultArray4[j];
                        NSMutableDictionary * dic = [dict objectForKey:@"master"];
                        NSString * birthStr = [[dic objectForKey:@"Birthday"] substringWithRange:NSMakeRange(0, 10)];
                        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
                        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
                        NSDate *birthDate = [dateFormatter dateFromString:birthStr];
                        NSString * realAge = [weak_self dateToOld:birthDate];
                        
                        NSMutableDictionary * dict1 = weak_self.resultArray4[j+1];
                        NSMutableDictionary * dic1 = [dict1 objectForKey:@"master"];
                        NSString * birthStr1 = [[dic1 objectForKey:@"Birthday"] substringWithRange:NSMakeRange(0, 10)];
                        NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc]init];
                        [dateFormatter1 setDateFormat:@"yyyy-MM-dd"];
                        NSDate *birthDate1 = [dateFormatter dateFromString:birthStr1];
                        NSString * realAge1 = [weak_self dateToOld:birthDate1];
                        //根据索引的`相邻两位`进行`比较`
                        if ([realAge integerValue] < [realAge1 integerValue]) {
                            
                            [weak_self.resultArray4 exchangeObjectAtIndex:j withObjectAtIndex:j+1];
                        }
                        
                    }
                }
            }
            if ([weak_self.rightNowProjectId isEqual:@10]) {
                for (int i = 0; i < weak_self.resultArray10.count; ++i) {
                    
                    //遍历数组的每一个`索引`（不包括最后一个,因为比较的是j+1）
                    for (int j = 0; j < weak_self.resultArray10.count-1; ++j) {
                        NSMutableDictionary * dict = weak_self.resultArray10[j];
                        NSMutableDictionary * dic = [dict objectForKey:@"master"];
                        NSString * birthStr = [[dic objectForKey:@"Birthday"] substringWithRange:NSMakeRange(0, 10)];
                        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
                        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
                        NSDate *birthDate = [dateFormatter dateFromString:birthStr];
                        NSString * realAge = [weak_self dateToOld:birthDate];
                        
                        NSMutableDictionary * dict1 = weak_self.resultArray10[j+1];
                        NSMutableDictionary * dic1 = [dict1 objectForKey:@"master"];
                        NSString * birthStr1 = [[dic1 objectForKey:@"Birthday"] substringWithRange:NSMakeRange(0, 10)];
                        NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc]init];
                        [dateFormatter1 setDateFormat:@"yyyy-MM-dd"];
                        NSDate *birthDate1 = [dateFormatter dateFromString:birthStr1];
                        NSString * realAge1 = [weak_self dateToOld:birthDate1];
                        //根据索引的`相邻两位`进行`比较`
                        if ([realAge integerValue] > [realAge1 integerValue]) {
                            
                            [weak_self.resultArray10 exchangeObjectAtIndex:j withObjectAtIndex:j+1];
                        }
                        
                    }
                }
            }
            [weak_self.leftTableView reloadData];
            [weak_self.rightTableView reloadData];
            
        };
        return  cell;
        }
    }
    if (tableView ==self.rightTableView) {//右侧TABLEVIEW
        TestingCell * cell = [tableView dequeueReusableCellWithIdentifier:@"TestingCell"];
        if (cell ==nil) {
            cell = [[TestingCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TestingCell"];
        }
        switch ([self.rightNowProjectId integerValue]) {
            case 1:
                [cell relayoutCellWithDic:self.resultArray1[indexPath.row] andProjectId:self.rightNowProjectId];
                break;
            case 2:
                [cell relayoutCellWithDic:self.resultArray2[indexPath.row] andProjectId:self.rightNowProjectId];
                break;
            case 4:
                [cell relayoutCellWithDic:self.resultArray4[indexPath.row] andProjectId:self.rightNowProjectId];
                break;
            case 5:
                [cell relayoutCellWithDic:self.resultArray5[indexPath.row] andProjectId:self.rightNowProjectId];
                break;
            case 6:
                [cell relayoutCellWithDic:self.resultArray6[indexPath.row] andProjectId:self.rightNowProjectId];
                break;
            case 7:
                [cell relayoutCellWithDic:self.resultArray7[indexPath.row] andProjectId:self.rightNowProjectId];
                break;
            case 8:
                [cell relayoutCellWithDic:self.resultArray8[indexPath.row] andProjectId:self.rightNowProjectId];
                break;
            case 9:
                [cell relayoutCellWithDic:self.resultArray9[indexPath.row] andProjectId:self.rightNowProjectId];
                break;
            case 10:
                [cell relayoutCellWithDic:self.resultArray10[indexPath.row] andProjectId:self.rightNowProjectId];
                break;
            default:
                break;
        }
        
        
        //CELL代码块方法创建输入修改数据框
        __weak typeof(self) weak_self = self;
        cell.callinput = ^() {
            switch ([weak_self.rightNowProjectId integerValue]) {
                case 1:
                {
                    NSMutableDictionary * dict = weak_self.resultArray1[indexPath.row];
                    NSMutableDictionary * dic = [dict objectForKey:@"master"];
                    ChildModel * model = [ChildModel modelWithDictionary:dic];
                    [weak_self creatInputViewWithModel:model andinteger:indexPath.row];
                }
                    break;
                case 2:
                {
                    NSMutableDictionary * dict = weak_self.resultArray2[indexPath.row];
                    NSMutableDictionary * dic = [dict objectForKey:@"master"];
                    ChildModel * model = [ChildModel modelWithDictionary:dic];
                    [weak_self creatInputViewWithModel:model andinteger:indexPath.row];
                }
                    
                    break;
                case 4:
                {
                    NSMutableDictionary * dict = weak_self.resultArray4[indexPath.row];
                    NSMutableDictionary * dic = [dict objectForKey:@"master"];
                    ChildModel * model = [ChildModel modelWithDictionary:dic];
                    [weak_self creatInputViewWithModel:model andinteger:indexPath.row];
                }
                    
                    break;
                case 5:
                {
                    NSMutableDictionary * dict = weak_self.resultArray5[indexPath.row];
                    NSMutableDictionary * dic = [dict objectForKey:@"master"];
                    ChildModel * model = [ChildModel modelWithDictionary:dic];
                    [weak_self creatInputViewWithModel:model andinteger:indexPath.row];
                }
                    
                    break;
                case 6:
                {
                    NSMutableDictionary * dict = weak_self.resultArray6[indexPath.row];
                    NSMutableDictionary * dic = [dict objectForKey:@"master"];
                    ChildModel * model = [ChildModel modelWithDictionary:dic];
                    [weak_self creatInputViewWithModel:model andinteger:indexPath.row];
                }
                    
                    break;
                case 7:
                {
                    NSMutableDictionary * dict = weak_self.resultArray7[indexPath.row];
                    NSMutableDictionary * dic = [dict objectForKey:@"master"];
                    ChildModel * model = [ChildModel modelWithDictionary:dic];
                    [weak_self creatInputViewWithModel:model andinteger:indexPath.row];
                }
                    
                    break;
                case 8:
                {
                    NSMutableDictionary * dict = weak_self.resultArray8[indexPath.row];
                    NSMutableDictionary * dic = [dict objectForKey:@"master"];
                    ChildModel * model = [ChildModel modelWithDictionary:dic];
                    [weak_self creatInputViewWithModel:model andinteger:indexPath.row];
                }
                    
                    break;
                case 9:
                {
                    NSMutableDictionary * dict = weak_self.resultArray9[indexPath.row];
                    NSMutableDictionary * dic = [dict objectForKey:@"master"];
                    ChildModel * model = [ChildModel modelWithDictionary:dic];
                    [weak_self creatInputViewWithModel:model andinteger:indexPath.row];
                }
                    
                    break;
                case 10:
                {
                    NSMutableDictionary * dict = weak_self.resultArray10[indexPath.row];
                    NSMutableDictionary * dic = [dict objectForKey:@"master"];
                    ChildModel * model = [ChildModel modelWithDictionary:dic];
                    [weak_self creatInputViewWithModel:model andinteger:indexPath.row];
                }
                    
                    break;
                default:
                    break;
            }
            
        };

        return  cell;
    }
    return nil;

    
}

#pragma mark 输入数据框
-(void)creatInputViewWithModel:(ChildModel*)model andinteger:(NSInteger)intger{
    _rightNowModel = model;//当前MODEL ，用于修改本地数据
    _indexpathrow = intger;
    //蒙版
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    _backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _backView.backgroundColor = [UIColor blackColor];
    _backView.alpha = 0.52;
    [window addSubview:_backView];
    
    self.inputDataView = [[InputDataView alloc]init];
    self.inputDataView.inputTextField.delegate =self;
    self.inputDataView.inputTextField.keyboardType = UIKeyboardTypeNumberPad;
    self.inputDataView.childName.text = model.ChildName;

    //区分单位
    switch ([self.rightNowProjectId integerValue]) {
        case 1:
             self.inputDataView.unitLabel.text = @"厘米";
            self.inputDataView.projectLabel.text = @"身高";
            break;
        case 2:
            self.inputDataView.unitLabel.text = @"千克";
            self.inputDataView.projectLabel.text = @"体重";
            break;
        case 4:
            self.inputDataView.unitLabel.text = @"圈";
            self.inputDataView.projectLabel.text = @"15米节奏往返跑";
            break;
        case 5:
            self.inputDataView.unitLabel.text = @"个";
            self.inputDataView.projectLabel.text = @"仰卧卷腹";
            break;
        case 6:
            self.inputDataView.unitLabel.text = @"";
            self.inputDataView.projectLabel.text = @"俯卧背伸";
            break;
        case 7:
            self.inputDataView.unitLabel.text = @"个";
            self.inputDataView.projectLabel.text = @"90°俯卧撑";
            break;
        case 8:
            self.inputDataView.unitLabel.text = @"厘米";
            self.inputDataView.projectLabel.text = @"坐位体前屈";
            break;
        case 9:
            self.inputDataView.unitLabel.text = @"厘米";
            self.inputDataView.projectLabel.text = @"立定跳远";
            break;
        case 10:
            self.inputDataView.unitLabel.text = @"秒";
            self.inputDataView.projectLabel.text = @"走平衡木";
            break;
        default:
            break;
    }
    
    [self.inputDataView.unTestableBtn addTarget:self action:@selector(touchUnTestable) forControlEvents:UIControlEventTouchUpInside];
    [self.inputDataView.cancelBtn addTarget:self action:@selector(touchCancel) forControlEvents:UIControlEventTouchUpInside];
    [self.inputDataView.confirmBtn addTarget:self action:@selector(touchConfirm) forControlEvents:UIControlEventTouchUpInside];
    [window addSubview:self.inputDataView];
    [self.inputDataView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(window.mas_centerX);
        make.centerY.mas_equalTo(window.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(550*AutoScaleHorizontal, 300*AutoScaleVertical));
    }];
    
}
//无法测试
-(void)touchUnTestable{
    self.inputDataView.unTestableBtn.selected = !self.inputDataView.unTestableBtn.selected;
    if (self.inputDataView.unTestableBtn.selected ==YES) {
        self.inputDataView.projectLabel.textColor = UIColorFromRGB(167, 167, 167);
        self.inputDataView.unitLabel.textColor =UIColorFromRGB(167, 167, 167);
        self.inputDataView.inputTextField.userInteractionEnabled =NO;
        self.inputDataView.starLabel.textColor = UIColorFromRGB(167, 167, 167);
    }else{
        self.inputDataView.projectLabel.textColor = [UIColor blackColor];
        self.inputDataView.unitLabel.textColor =[UIColor blackColor];
        self.inputDataView.starLabel.textColor = [UIColor redColor];
        self.inputDataView.inputTextField.userInteractionEnabled =YES;
    }
}
//取消
-(void)touchCancel{
    [self.backView removeFromSuperview];
    [self.inputDataView removeFromSuperview];
}
//确认修改
-(void)touchConfirm{
    
    
    if (self.inputDataView.unTestableBtn.selected == NO && self.inputDataView.inputTextField.text.length==0) {
        [self.inputDataView.inputTextField resignFirstResponder];
        [self showMessage:@"请输入测试数据"];
    }else if([self.rightNowProjectId isEqual:@1]){
        if (([self.inputDataView.inputTextField.text integerValue]<50 || [self.inputDataView.inputTextField.text integerValue]>200)&&self.inputDataView.unTestableBtn.selected == NO) {
            [self showMessage:@"当前项目取值范围是50～200厘米"];
            [self.inputDataView.inputTextField resignFirstResponder];
        }else{
            [self touchConFirmRight];
        }
    }else if([self.rightNowProjectId isEqual:@2]){
        if (([self.inputDataView.inputTextField.text integerValue]<5 || [self.inputDataView.inputTextField.text integerValue]>100)&&self.inputDataView.unTestableBtn.selected == NO) {
            [self showMessage:@"当前项目取值范围是5～100千克"];
            [self.inputDataView.inputTextField resignFirstResponder];
        }else{
            [self touchConFirmRight];
        }
    }else if([self.rightNowProjectId isEqual:@4]){
        if (([self.inputDataView.inputTextField.text integerValue]<=0 || [self.inputDataView.inputTextField.text integerValue]>207)&&self.inputDataView.unTestableBtn.selected == NO) {
            [self showMessage:@"当前项目取值范围是0～207圈"];
            [self.inputDataView.inputTextField resignFirstResponder];
        }else{
            [self touchConFirmRight];
        }
    }else if([self.rightNowProjectId isEqual:@5]){
        if (([self.inputDataView.inputTextField.text integerValue]<=0 || [self.inputDataView.inputTextField.text integerValue]>75)&&self.inputDataView.unTestableBtn.selected == NO) {
            [self showMessage:@"当前项目取值范围是0～75个"];
            [self.inputDataView.inputTextField resignFirstResponder];
        }else{
            [self touchConFirmRight];
        }
    }else if([self.rightNowProjectId isEqual:@6]){
        if (([self.inputDataView.inputTextField.text integerValue]<=0 || [self.inputDataView.inputTextField.text integerValue]>12)&&self.inputDataView.unTestableBtn.selected == NO) {
            [self showMessage:@"当前项目取值范围是0～12"];
            [self.inputDataView.inputTextField resignFirstResponder];
        }else{
            [self touchConFirmRight];
        }
    }else if([self.rightNowProjectId isEqual:@7]){
        if (([self.inputDataView.inputTextField.text integerValue]<=0 || [self.inputDataView.inputTextField.text integerValue]>100)&&self.inputDataView.unTestableBtn.selected == NO) {
            [self showMessage:@"当前项目取值范围是0～100个"];
            [self.inputDataView.inputTextField resignFirstResponder];
        }else{
            [self touchConFirmRight];
        }
    }else if([self.rightNowProjectId isEqual:@8]){
        if (([self.inputDataView.inputTextField.text integerValue]<-20 || [self.inputDataView.inputTextField.text integerValue]>40)&&self.inputDataView.unTestableBtn.selected == NO) {
            [self showMessage:@"当前项目取值范围是-20～40厘米"];
            [self.inputDataView.inputTextField resignFirstResponder];
        }else{
            [self touchConFirmRight];
        }
    }else if([self.rightNowProjectId isEqual:@9]){
        if (([self.inputDataView.inputTextField.text integerValue]<20 || [self.inputDataView.inputTextField.text integerValue]>320)&&self.inputDataView.unTestableBtn.selected == NO) {
            [self showMessage:@"当前项目取值范围是20～320厘米"];
            [self.inputDataView.inputTextField resignFirstResponder];
        }else{
            [self touchConFirmRight];
        }
    }else if([self.rightNowProjectId isEqual:@10]){
        if (([self.inputDataView.inputTextField.text integerValue]<2 || [self.inputDataView.inputTextField.text integerValue]>60)&&self.inputDataView.unTestableBtn.selected == NO) {
            [self showMessage:@"当前项目取值范围是2～60秒"];
            [self.inputDataView.inputTextField resignFirstResponder];
        }else{
            [self touchConFirmRight];
        }
    }
}
//没有超出范围

-(void)touchConFirmRight{
    
        
        NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
        NSMutableArray * testArr = [_allDataDictionary objectForKey:@"TestResult"];//拿到目前全部数据源的testresult
        for (NSMutableDictionary * dict in testArr) {
            NSMutableDictionary * dic = [dict objectForKey:@"master"];//master
            NSMutableArray * detailArr = [dict objectForKey:@"detailList"];//detail
            if ([[dic objectForKey:@"Id"]isEqual:self.rightNowModel.Id]) {
                int hadNew = 0;//用来查看是否有原有数据
                for (NSMutableDictionary * detailDic in detailArr) {
                    if ([[detailDic objectForKey:@"ProjectId"]isEqual:self.rightNowProjectId]) {//如果有相等的说明之前有此项目数据
                        hadNew += 1;
                        if (self.inputDataView.unTestableBtn.selected == YES) {
                            NSNumber* yesNum = [NSNumber numberWithBool:YES];
                            //判断此项之前是否是ISTEST ，不是的话， 给一个VALUECHANGE字段
                            if ([[detailDic objectForKey:@"IsTest"] boolValue]) {
                                [detailDic setObject:@"1" forKey:@"1900V"];
                            }
                            [detailDic setObject:yesNum forKey:@"IsTest"];
                        }else{
                            //判断此项之前是否是ISTEST ，不是的话， 给一个VALUECHANGE字段
                            if (![[detailDic objectForKey:@"IsTest"] boolValue]) {
                                [detailDic setObject:@"1" forKey:@"1900V"];
                            }
                            NSNumber* yesNum = [NSNumber numberWithBool:NO];
                            [detailDic setObject:yesNum forKey:@"IsTest"];
                            NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
                            [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
                            NSNumber *numTemp = [numberFormatter numberFromString:self.inputDataView.inputTextField.text];
                            //判断此项result change ，是， 给一个VALUECHANGE字段
                            if (![[detailDic objectForKey:@"Result"]isEqual:numTemp] ) {
                                [detailDic setObject:@"1" forKey:@"1900V"];
                            }
                            [detailDic setObject:numTemp forKey:@"Result"];
                        }
                    }
                }
                // 没有数据  新建
                if (hadNew == 0) {
                    NSMutableDictionary * newProDic = [[NSMutableDictionary alloc]init];
                    if (self.inputDataView.unTestableBtn.selected == YES) {
                        NSNumber* yesNum = [NSNumber numberWithBool:YES];
                        [newProDic setObject:yesNum forKey:@"IsTest"];
                    }else{
                        NSNumber* yesNum = [NSNumber numberWithBool:NO];
                        [newProDic setObject:yesNum forKey:@"IsTest"];
                        NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
                        [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
                        NSNumber *numTemp = [numberFormatter numberFromString:self.inputDataView.inputTextField.text];
                        [newProDic setObject:numTemp forKey:@"Result"];
                    }
                    [newProDic setObject:self.rightNowModel.Id forKey:@"MasterId"];
                    //区分单位
                    switch ([self.rightNowProjectId integerValue]) {
                        case 1:
                            [newProDic setObject:@"身高" forKey:@"Name"];
                            [newProDic setObject:@1 forKey:@"ProjectId"];
                            break;
                        case 2:
                            [newProDic setObject:@"体重" forKey:@"Name"];
                            [newProDic setObject:@2 forKey:@"ProjectId"];
                            
                            break;
                        case 4:
                            [newProDic setObject:@"20米节奏往返跑" forKey:@"Name"];
                            [newProDic setObject:@4 forKey:@"ProjectId"];
                            break;
                        case 5:
                            [newProDic setObject:@"仰卧卷腹" forKey:@"Name"];
                            [newProDic setObject:@5 forKey:@"ProjectId"];
                            
                            break;
                        case 6:
                            [newProDic setObject:@"俯卧背伸" forKey:@"Name"];
                            [newProDic setObject:@6 forKey:@"ProjectId"];
                            
                            break;
                        case 7:
                            [newProDic setObject:@"90°俯卧撑" forKey:@"Name"];
                            [newProDic setObject:@7 forKey:@"ProjectId"];
                            
                            break;
                        case 8:
                            [newProDic setObject:@"坐位体前屈" forKey:@"Name"];
                            [newProDic setObject:@8 forKey:@"ProjectId"];
                            
                            break;
                        case 9:
                            [newProDic setObject:@"立定跳远" forKey:@"Name"];
                            [newProDic setObject:@9 forKey:@"ProjectId"];
                            
                            break;
                        case 10:
                            [newProDic setObject:@"走平衡木" forKey:@"Name"];
                            [newProDic setObject:@10 forKey:@"ProjectId"];
                            break;
                        default:
                            break;
                    }
                    //新ID
                    NSInteger newbabyId = [[user objectForKey:@"CreatProjectId"]integerValue]-1;
                    NSNumber * babyId = [NSNumber numberWithInt:newbabyId];
                    [user removeObjectForKey:@"CreatProjectId"];
                    [user setObject:babyId forKey:@"CreatProjectId"];
                    [newProDic setObject:babyId forKey:@"Id"];
                    [detailArr addObject:newProDic];
                }
                
            }
        }
        [_allDataDictionary removeObjectForKey:@"TestResult"];
        [_allDataDictionary setObject:testArr forKey:@"TestResult"];
        NSData * data = [NSJSONSerialization dataWithJSONObject:_allDataDictionary options:NSJSONWritingPrettyPrinted error:nil];
        NSString * allStr = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        [user removeObjectForKey:@"AllData"];
        [user setObject:allStr forKey:@"AllData"];
        [user setObject:@"yes" forKey:@"ValueChanged"];
        [user synchronize];
        
        //更新页面数据
        switch ([self.rightNowProjectId integerValue]) {
            case 1:
            {
                
                NSMutableDictionary * dict = self.resultArray1[_indexpathrow];
                NSMutableDictionary * dic = [dict objectForKey:@"detailDic"];
                if ([dic objectForKey:@"ProjectId"]!=nil) {
                    [dic setObject:self.rightNowProjectId forKey:@"ProjectId"];
                    
                    if (self.inputDataView.unTestableBtn.selected ==YES) {
                        NSNumber* yesNum =[NSNumber numberWithBool:YES];
                        [dic setObject:yesNum forKey:@"IsTest"];
                    }else{
                        NSNumber* yesNum =[NSNumber numberWithBool:NO];
                        [dic setObject:yesNum forKey:@"IsTest"];
                        NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
                        [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
                        NSNumber *numTemp = [numberFormatter numberFromString:self.inputDataView.inputTextField.text];
                        [dic setObject:numTemp forKey:@"Result"];
                    }
                    [dict removeObjectForKey:@"detailDic"];
                    [dict setObject:dic forKey:@"detailDic"];
                    [self.resultArray1 replaceObjectAtIndex:_indexpathrow withObject:dict];
                    //                [self.resultArray1 removeObject:dict];
                    
                }
                else{
                    NSMutableDictionary * newDic= [[NSMutableDictionary alloc]init];
                    if (self.inputDataView.unTestableBtn.selected ==YES) {
                        NSNumber* yesNum =[NSNumber numberWithBool:YES];
                        [newDic setObject:yesNum forKey:@"IsTest"];
                    }else{
                        NSNumber* yesNum =[NSNumber numberWithBool:NO];
                        [newDic setObject:yesNum forKey:@"IsTest"];
                        NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
                        [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
                        NSNumber *numTemp = [numberFormatter numberFromString:self.inputDataView.inputTextField.text];
                        [newDic setObject:numTemp forKey:@"Result"];
                    }
                    [newDic setObject:self.rightNowProjectId forKey:@"ProjectId"];
                    [dict setObject:newDic forKey:@"detailDic"];
                    [self.resultArray1 replaceObjectAtIndex:_indexpathrow withObject:dict];
                    //                [self.resultArray1 removeObject:dict];
                }
            }
                break;
            case 2:
            {
                NSMutableDictionary * dict = self.resultArray2[_indexpathrow];
                NSMutableDictionary * dic = [dict objectForKey:@"detailDic"];
                if ([dic objectForKey:@"ProjectId"]!=nil) {
                    [dic setObject:self.rightNowProjectId forKey:@"ProjectId"];
                    
                    if (self.inputDataView.unTestableBtn.selected ==YES) {
                        NSNumber* yesNum =[NSNumber numberWithBool:YES];
                        [dic setObject:yesNum forKey:@"IsTest"];
                    }else{
                        NSNumber* yesNum =[NSNumber numberWithBool:NO];
                        [dic setObject:yesNum forKey:@"IsTest"];
                        NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
                        [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
                        NSNumber *numTemp = [numberFormatter numberFromString:self.inputDataView.inputTextField.text];
                        [dic setObject:numTemp forKey:@"Result"];
                    }
                    [dict removeObjectForKey:@"detailDic"];
                    [dict setObject:dic forKey:@"detailDic"];
                    [self.resultArray2 replaceObjectAtIndex:_indexpathrow withObject:dict];
                }else{
                    NSMutableDictionary * newDic= [[NSMutableDictionary alloc]init];
                    if (self.inputDataView.unTestableBtn.selected ==YES) {
                        NSNumber* yesNum =[NSNumber numberWithBool:YES];
                        [newDic setObject:yesNum forKey:@"IsTest"];
                    }else{
                        NSNumber* yesNum =[NSNumber numberWithBool:NO];
                        [newDic setObject:yesNum forKey:@"IsTest"];
                        NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
                        [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
                        NSNumber *numTemp = [numberFormatter numberFromString:self.inputDataView.inputTextField.text];
                        [newDic setObject:numTemp forKey:@"Result"];
                    }
                    [newDic setObject:self.rightNowProjectId forKey:@"ProjectId"];
                    [dict setObject:newDic forKey:@"detailDic"];
                    [self.resultArray2 replaceObjectAtIndex:_indexpathrow withObject:dict];
                }
            }
                
                break;
            case 4:
            {
                NSMutableDictionary * dict = self.resultArray4[_indexpathrow];
                NSMutableDictionary * dic = [dict objectForKey:@"detailDic"];
                if ([dic objectForKey:@"ProjectId"] !=nil) {
                    [dic setObject:self.rightNowProjectId forKey:@"ProjectId"];
                    
                    if (self.inputDataView.unTestableBtn.selected ==YES) {
                        NSNumber* yesNum =[NSNumber numberWithBool:YES];
                        [dic setObject:yesNum forKey:@"IsTest"];
                    }else{
                        NSNumber* yesNum =[NSNumber numberWithBool:NO];
                        [dic setObject:yesNum forKey:@"IsTest"];
                        NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
                        [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
                        NSNumber *numTemp = [numberFormatter numberFromString:self.inputDataView.inputTextField.text];
                        [dic setObject:numTemp forKey:@"Result"];
                    }
                    [dict removeObjectForKey:@"detailDic"];
                    [dict setObject:dic forKey:@"detailDic"];
                    [self.resultArray4 replaceObjectAtIndex:_indexpathrow withObject:dict];
                }
                else{
                    NSMutableDictionary * newDic= [[NSMutableDictionary alloc]init];
                    if (self.inputDataView.unTestableBtn.selected ==YES) {
                        NSNumber* yesNum =[NSNumber numberWithBool:YES];
                        [newDic setObject:yesNum forKey:@"IsTest"];
                    }else{
                        NSNumber* yesNum =[NSNumber numberWithBool:NO];
                        [newDic setObject:yesNum forKey:@"IsTest"];
                        NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
                        [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
                        NSNumber *numTemp = [numberFormatter numberFromString:self.inputDataView.inputTextField.text];
                        [newDic setObject:numTemp forKey:@"Result"];
                    }
                    [newDic setObject:self.rightNowProjectId forKey:@"ProjectId"];
                    [dict setObject:newDic forKey:@"detailDic"];
                    [self.resultArray4 replaceObjectAtIndex:_indexpathrow withObject:dict];
                }
            }
                
                break;
            case 5:
            {
                NSMutableDictionary * dict = self.resultArray5[_indexpathrow];
                NSMutableDictionary * dic = [dict objectForKey:@"detailDic"];
                if ([dic objectForKey:@"ProjectId"]!=nil) {
                    [dic setObject:self.rightNowProjectId forKey:@"ProjectId"];
                    
                    if (self.inputDataView.unTestableBtn.selected ==YES) {
                        NSNumber* yesNum =[NSNumber numberWithBool:YES];
                        [dic setObject:yesNum forKey:@"IsTest"];
                    }else{
                        NSNumber* yesNum =[NSNumber numberWithBool:NO];
                        [dic setObject:yesNum forKey:@"IsTest"];
                        NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
                        [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
                        NSNumber *numTemp = [numberFormatter numberFromString:self.inputDataView.inputTextField.text];
                        [dic setObject:numTemp forKey:@"Result"];
                    }
                    [dict removeObjectForKey:@"detailDic"];
                    [dict setObject:dic forKey:@"detailDic"];
                    [self.resultArray5 replaceObjectAtIndex:_indexpathrow withObject:dict];
                }
                else{
                    NSMutableDictionary * newDic= [[NSMutableDictionary alloc]init];
                    if (self.inputDataView.unTestableBtn.selected ==YES) {
                        NSNumber* yesNum =[NSNumber numberWithBool:YES];
                        [newDic setObject:yesNum forKey:@"IsTest"];
                    }else{
                        NSNumber* yesNum =[NSNumber numberWithBool:NO];
                        [newDic setObject:yesNum forKey:@"IsTest"];
                        NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
                        [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
                        NSNumber *numTemp = [numberFormatter numberFromString:self.inputDataView.inputTextField.text];
                        [newDic setObject:numTemp forKey:@"Result"];
                    }
                    [newDic setObject:self.rightNowProjectId forKey:@"ProjectId"];
                    [dict setObject:newDic forKey:@"detailDic"];
                    [self.resultArray5 replaceObjectAtIndex:_indexpathrow withObject:dict];
                }
            }
                
                break;
            case 6:
            {
                NSMutableDictionary * dict = self.resultArray6[_indexpathrow];
                NSMutableDictionary * dic = [dict objectForKey:@"detailDic"];
                if ([dic objectForKey:@"ProjectId"] !=nil) {
                    [dic setObject:self.rightNowProjectId forKey:@"ProjectId"];
                    
                    if (self.inputDataView.unTestableBtn.selected ==YES) {
                        NSNumber* yesNum =[NSNumber numberWithBool:YES];
                        [dic setObject:yesNum forKey:@"IsTest"];
                    }else{
                        NSNumber* yesNum =[NSNumber numberWithBool:NO];
                        [dic setObject:yesNum forKey:@"IsTest"];
                        NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
                        [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
                        NSNumber *numTemp = [numberFormatter numberFromString:self.inputDataView.inputTextField.text];
                        [dic setObject:numTemp forKey:@"Result"];
                    }
                    [dict removeObjectForKey:@"detailDic"];
                    [dict setObject:dic forKey:@"detailDic"];
                    [self.resultArray6 replaceObjectAtIndex:_indexpathrow withObject:dict];
                }
                else{
                    NSMutableDictionary * newDic= [[NSMutableDictionary alloc]init];
                    if (self.inputDataView.unTestableBtn.selected ==YES) {
                        NSNumber* yesNum =[NSNumber numberWithBool:YES];
                        [newDic setObject:yesNum forKey:@"IsTest"];
                    }else{
                        NSNumber* yesNum =[NSNumber numberWithBool:NO];
                        [newDic setObject:yesNum forKey:@"IsTest"];
                        NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
                        [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
                        NSNumber *numTemp = [numberFormatter numberFromString:self.inputDataView.inputTextField.text];
                        [newDic setObject:numTemp forKey:@"Result"];
                    }
                    [newDic setObject:self.rightNowProjectId forKey:@"ProjectId"];
                    [dict setObject:newDic forKey:@"detailDic"];
                    [self.resultArray6 replaceObjectAtIndex:_indexpathrow withObject:dict];
                }
            }
                
                break;
            case 7:
            {
                NSMutableDictionary * dict = self.resultArray7[_indexpathrow];
                NSMutableDictionary * dic = [dict objectForKey:@"detailDic"];
                if ([dic objectForKey:@"ProjectId"] !=nil) {
                    [dic setObject:self.rightNowProjectId forKey:@"ProjectId"];
                    if (self.inputDataView.unTestableBtn.selected ==YES) {
                        NSNumber* yesNum =[NSNumber numberWithBool:YES];
                        [dic setObject:yesNum forKey:@"IsTest"];
                    }else{
                        NSNumber* yesNum =[NSNumber numberWithBool:NO];
                        [dic setObject:yesNum forKey:@"IsTest"];
                        NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
                        [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
                        NSNumber *numTemp = [numberFormatter numberFromString:self.inputDataView.inputTextField.text];
                        [dic setObject:numTemp forKey:@"Result"];
                    }
                    [dict removeObjectForKey:@"detailDic"];
                    [dict setObject:dic forKey:@"detailDic"];
                    [self.resultArray7 replaceObjectAtIndex:_indexpathrow withObject:dict];
                }
                else{
                    NSMutableDictionary * newDic= [[NSMutableDictionary alloc]init];
                    if (self.inputDataView.unTestableBtn.selected ==YES) {
                        NSNumber* yesNum =[NSNumber numberWithBool:YES];
                        [newDic setObject:yesNum forKey:@"IsTest"];
                    }else{
                        NSNumber* yesNum =[NSNumber numberWithBool:NO];
                        [newDic setObject:yesNum forKey:@"IsTest"];
                        NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
                        [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
                        NSNumber *numTemp = [numberFormatter numberFromString:self.inputDataView.inputTextField.text];
                        [newDic setObject:numTemp forKey:@"Result"];
                    }
                    [newDic setObject:self.rightNowProjectId forKey:@"ProjectId"];
                    [dict setObject:newDic forKey:@"detailDic"];
                    [self.resultArray7 replaceObjectAtIndex:_indexpathrow withObject:dict];
                }
            }
                
                break;
            case 8:
            {
                NSMutableDictionary * dict = self.resultArray8[_indexpathrow];
                NSMutableDictionary * dic = [dict objectForKey:@"detailDic"];
                if ([dic objectForKey:@"ProjectId"] !=nil) {
                    [dic setObject:self.rightNowProjectId forKey:@"ProjectId"];
                    
                    if (self.inputDataView.unTestableBtn.selected ==YES) {
                        NSNumber* yesNum =[NSNumber numberWithBool:YES];
                        [dic setObject:yesNum forKey:@"IsTest"];
                    }else{
                        NSNumber* yesNum =[NSNumber numberWithBool:NO];
                        [dic setObject:yesNum forKey:@"IsTest"];
                        
                        NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
                        [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
                        NSNumber *numTemp = [numberFormatter numberFromString:self.inputDataView.inputTextField.text];
                        [dic setObject:numTemp forKey:@"Result"];
                    }
                    [dict removeObjectForKey:@"detailDic"];
                    [dict setObject:dic forKey:@"detailDic"];
                    [self.resultArray8 replaceObjectAtIndex:_indexpathrow withObject:dict];
                }else{
                    NSMutableDictionary * newDic= [[NSMutableDictionary alloc]init];
                    if (self.inputDataView.unTestableBtn.selected ==YES) {
                        NSNumber* yesNum =[NSNumber numberWithBool:YES];
                        [newDic setObject:yesNum forKey:@"IsTest"];
                    }else{
                        NSNumber* yesNum =[NSNumber numberWithBool:NO];
                        [newDic setObject:yesNum forKey:@"IsTest"];
                        
                        NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
                        [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
                        NSNumber *numTemp = [numberFormatter numberFromString:self.inputDataView.inputTextField.text];
                        [newDic setObject:numTemp forKey:@"Result"];
                    }
                    [newDic setObject:self.rightNowProjectId forKey:@"ProjectId"];
                    [dict setObject:newDic forKey:@"detailDic"];
                    [self.resultArray8 replaceObjectAtIndex:_indexpathrow withObject:dict];
                }
            }
                
                break;
            case 9:
            {
                NSMutableDictionary * dict = self.resultArray9[_indexpathrow];
                NSMutableDictionary * dic = [dict objectForKey:@"detailDic"];
                if ([dic objectForKey:@"ProjectId"] !=nil) {
                    [dic setObject:self.rightNowProjectId forKey:@"ProjectId"];
                    
                    if (self.inputDataView.unTestableBtn.selected ==YES) {
                        NSNumber* yesNum =[NSNumber numberWithBool:YES];
                        [dic setObject:yesNum forKey:@"IsTest"];
                    }else{
                        NSNumber* yesNum =[NSNumber numberWithBool:NO];
                        [dic setObject:yesNum forKey:@"IsTest"];
                        NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
                        [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
                        NSNumber *numTemp = [numberFormatter numberFromString:self.inputDataView.inputTextField.text];
                        [dic setObject:numTemp forKey:@"Result"];
                    }
                    [dict removeObjectForKey:@"detailDic"];
                    [dict setObject:dic forKey:@"detailDic"];
                    [self.resultArray9 replaceObjectAtIndex:_indexpathrow withObject:dict];
                }
                else{
                    NSMutableDictionary * newDic= [[NSMutableDictionary alloc]init];
                    if (self.inputDataView.unTestableBtn.selected ==YES) {
                        NSNumber* yesNum =[NSNumber numberWithBool:YES];
                        [newDic setObject:yesNum forKey:@"IsTest"];
                    }else{
                        NSNumber* yesNum =[NSNumber numberWithBool:NO];
                        [newDic setObject:yesNum forKey:@"IsTest"];
                        NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
                        [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
                        NSNumber *numTemp = [numberFormatter numberFromString:self.inputDataView.inputTextField.text];
                        [newDic setObject:numTemp forKey:@"Result"];
                    }
                    [newDic setObject:self.rightNowProjectId forKey:@"ProjectId"];
                    [dict setObject:newDic forKey:@"detailDic"];
                    [self.resultArray9 replaceObjectAtIndex:_indexpathrow withObject:dict];
                }
            }
                
                break;
            case 10:
            {
                NSMutableDictionary * dict = self.resultArray10[_indexpathrow];
                NSMutableDictionary * dic = [dict objectForKey:@"detailDic"];
                if ([dic objectForKey:@"ProjectId"]!=nil) {
                    
                    [dic setObject:self.rightNowProjectId forKey:@"ProjectId"];
                    if (self.inputDataView.unTestableBtn.selected ==YES) {
                        NSNumber* yesNum =[NSNumber numberWithBool:YES];
                        [dic setObject:yesNum forKey:@"IsTest"];
                    }else{
                        NSNumber* yesNum =[NSNumber numberWithBool:NO];
                        [dic setObject:yesNum forKey:@"IsTest"];
                        NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
                        [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
                        NSNumber *numTemp = [numberFormatter numberFromString:self.inputDataView.inputTextField.text];
                        [dic setObject:numTemp forKey:@"Result"];
                    }
                    [dict removeObjectForKey:@"detailDic"];
                    [dict setObject:dic forKey:@"detailDic"];
                    [self.resultArray10 replaceObjectAtIndex:_indexpathrow withObject:dict];
                }
                else{
                    NSMutableDictionary * newDic= [[NSMutableDictionary alloc]init];
                    if (self.inputDataView.unTestableBtn.selected ==YES) {
                        NSNumber* yesNum =[NSNumber numberWithBool:YES];
                        [newDic setObject:yesNum forKey:@"IsTest"];
                    }else{
                        NSNumber* yesNum =[NSNumber numberWithBool:NO];
                        [newDic setObject:yesNum forKey:@"IsTest"];
                        NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
                        [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
                        NSNumber *numTemp = [numberFormatter numberFromString:self.inputDataView.inputTextField.text];
                        [newDic setObject:numTemp forKey:@"Result"];
                    }
                    [newDic setObject:self.rightNowProjectId forKey:@"ProjectId"];
                    [dict setObject:newDic forKey:@"detailDic"];
                    [self.resultArray10 replaceObjectAtIndex:_indexpathrow withObject:dict];
                }
            }
                break;
            default:
                break;
        }
        if (self.UntestBtn.selected ==YES) {
            [self pressUnTest];
        }else{
            [self.rightTableView reloadData];
        }
        
        //    [self.rightTableView reloadData];
        [self.backView removeFromSuperview];
        [self.inputDataView removeFromSuperview];
        [self changeUntestWithNum:[self.rightNowProjectId integerValue]];
    
}
#pragma mark 全部未测按钮切换数据
//未测选择
-(void)pressUnTest{
    [self.resultArray1 removeAllObjects];
    [self.resultArray2 removeAllObjects];
    [self.resultArray4 removeAllObjects];
    [self.resultArray5 removeAllObjects];
    [self.resultArray6 removeAllObjects];
    [self.resultArray7 removeAllObjects];
    [self.resultArray8 removeAllObjects];
    [self.resultArray9 removeAllObjects];
    [self.resultArray10 removeAllObjects];
    
    NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
    
    NSMutableArray * testArray = [NSMutableArray arrayWithArray: [_allDataDictionary objectForKey:@"TestResult"]];//有结果的数据
    //本次营销计划选中的人(通过MASTER ID  判断)
    NSString * FS = [user objectForKey:@"finalSaleDic"];
    NSData * jsonData = [FS dataUsingEncoding:NSUTF8StringEncoding];
    NSMutableDictionary * mdic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
    NSMutableArray * masterIdArray = [mdic objectForKey:@"finalSaleDic"];
    
    for (NSNumber * iidd in masterIdArray) {//循环遍历已选中的孩子ID
        
        for (NSMutableDictionary * dic in testArray) {//循环遍历testResult
            NSMutableDictionary * masterdic = [dic objectForKey:@"master"];
            NSMutableArray * detailArr = [dic objectForKey:@"detailList"];
            
            if ([[masterdic objectForKey:@"Id"]isEqual:iidd]) {//查找选中孩子的MASTER和RESULT
                //            [self.resultArray addObject:dic];//将整个孩子的RESULT都加入数据源
                int a = 1;
                for (DetailListModel * mod in self.projectArray) {//总共九个项目，创建9组对象
                    
                    NSMutableDictionary * resultDic = [[NSMutableDictionary alloc]init];//对象字典（master.detailDic）
                    [resultDic setObject:masterdic forKey:@"master"];
                    for (NSMutableDictionary * dict in detailArr) {//遍历当前项目有数据的话DETAILDIC为数据。否则为空
                        if ([[dict objectForKey:@"ProjectId"]isEqual:mod.ProjectId]) {
                            [resultDic setObject:dict forKey:@"detailDic"];
                        }
                    }
                    if ([resultDic objectForKey:@"detailDic"] ==nil) {//否则为空
                        NSMutableDictionary * newDetai =[[NSMutableDictionary alloc]init];
                        [resultDic setObject:newDetai forKey:@"detailDic"];
                    }
                    //分别按ID 将对象加入数组源
                    if (a ==1) {
                        [self.resultArray1 addObject:resultDic];
                    }
                    if (a ==2) {
                        [self.resultArray2 addObject:resultDic];
                    }
                    if (a ==3) {
                        [self.resultArray4 addObject:resultDic];
                    }
                    if (a ==4) {
                        [self.resultArray5 addObject:resultDic];
                    }
                    if (a ==5) {
                        [self.resultArray6 addObject:resultDic];
                    }
                    if (a ==6) {
                        [self.resultArray7 addObject:resultDic];
                    }
                    if (a ==7) {
                        [self.resultArray8 addObject:resultDic];
                    }
                    if (a ==8) {
                        [self.resultArray9 addObject:resultDic];
                    }
                    if (a ==9) {
                        [self.resultArray10 addObject:resultDic];
                    }
                    a++;
                    
                }
            }
        }
    }

    for (NSMutableDictionary * dict in self.resultArray1.reverseObjectEnumerator) {
        NSMutableDictionary * dic = [dict objectForKey:@"detailDic"];
        if ([dic objectForKey:@"ProjectId"]!=nil) {
            [self.resultArray1 removeObject:dict];
        }
    }
    for (NSMutableDictionary * dict in self.resultArray2.reverseObjectEnumerator) {
        NSMutableDictionary * dic = [dict objectForKey:@"detailDic"];
        if ([dic objectForKey:@"ProjectId"]!=nil) {
            [self.resultArray2 removeObject:dict];
        }
    }
    for (NSMutableDictionary * dict in self.resultArray4.reverseObjectEnumerator) {
        NSMutableDictionary * dic = [dict objectForKey:@"detailDic"];
        if ([dic objectForKey:@"ProjectId"]!=nil) {
            [self.resultArray4 removeObject:dict];
        }
    }
    for (NSMutableDictionary * dict in self.resultArray5.reverseObjectEnumerator) {
        NSMutableDictionary * dic = [dict objectForKey:@"detailDic"];
        if ([dic objectForKey:@"ProjectId"]!=nil) {
            [self.resultArray5 removeObject:dict];
        }
    }
    for (NSMutableDictionary * dict in self.resultArray6.reverseObjectEnumerator) {
        NSMutableDictionary * dic = [dict objectForKey:@"detailDic"];
        if ([dic objectForKey:@"ProjectId"]!=nil) {
            [self.resultArray6 removeObject:dict];
        }
    }
    for (NSMutableDictionary * dict in self.resultArray7.reverseObjectEnumerator) {
        NSMutableDictionary * dic = [dict objectForKey:@"detailDic"];
        if ([dic objectForKey:@"ProjectId"]!=nil) {
            [self.resultArray7 removeObject:dict];
        }
    }
    for (NSMutableDictionary * dict in self.resultArray8.reverseObjectEnumerator) {
        NSMutableDictionary * dic = [dict objectForKey:@"detailDic"];
        if ([dic objectForKey:@"ProjectId"]!=nil && ![[dic objectForKey:@"Result"]isEqual:@-111]) {
            [self.resultArray8 removeObject:dict];
        }
    }
    for (NSMutableDictionary * dict in self.resultArray9.reverseObjectEnumerator) {
        NSMutableDictionary * dic = [dict objectForKey:@"detailDic"];
        if ([dic objectForKey:@"ProjectId"]!=nil) {
            [self.resultArray9 removeObject:dict];
        }
    }
    for (NSMutableDictionary * dict in self.resultArray10.reverseObjectEnumerator) {
        NSMutableDictionary * dic = [dict objectForKey:@"detailDic"];
        if ([dic objectForKey:@"ProjectId"]!=nil) {
            [self.resultArray10 removeObject:dict];
        }
    }
    
    //如果是20米和平衡木 对数据源根据年龄排序
    
    
    
    
    if ([self.rightNowProjectId isEqual:@4]) {
        
        for (NSMutableDictionary * deletDic in self.resultArray4.reverseObjectEnumerator) {
            NSMutableDictionary * dic = [deletDic objectForKey:@"master"];
            NSString * birthStr = [[dic objectForKey:@"Birthday"] substringWithRange:NSMakeRange(0, 10)];
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
            [dateFormatter setDateFormat:@"yyyy-MM-dd"];
            NSDate *birthDate = [dateFormatter dateFromString:birthStr];
            NSString * realAge = [self dateToOld:birthDate];
            if ([realAge integerValue]<=8) {
                [self.resultArray4 removeObject:deletDic];
            }
            
        }
        
        for (int i = 0; i <self.resultArray4.count; ++i) {
            
            //遍历数组的每一个`索引`（不包括最后一个,因为比较的是j+1）
            for (int j = 0; j <self.resultArray4.count-1; ++j) {
                NSMutableDictionary * dict = self.resultArray4[j];
                NSMutableDictionary * dic = [dict objectForKey:@"master"];
                NSString * birthStr = [[dic objectForKey:@"Birthday"] substringWithRange:NSMakeRange(0, 10)];
                NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
                [dateFormatter setDateFormat:@"yyyy-MM-dd"];
                NSDate *birthDate = [dateFormatter dateFromString:birthStr];
                NSString * realAge = [self dateToOld:birthDate];
                
                NSMutableDictionary * dict1 = self.resultArray4[j+1];
                NSMutableDictionary * dic1 = [dict1 objectForKey:@"master"];
                NSString * birthStr1 = [[dic1 objectForKey:@"Birthday"] substringWithRange:NSMakeRange(0, 10)];
                NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc]init];
                [dateFormatter1 setDateFormat:@"yyyy-MM-dd"];
                NSDate *birthDate1 = [dateFormatter dateFromString:birthStr1];
                NSString * realAge1 = [self dateToOld:birthDate1];
                //根据索引的`相邻两位`进行`比较`
                if ([realAge integerValue] < [realAge1 integerValue]) {
                    
                    [self.resultArray4 exchangeObjectAtIndex:j withObjectAtIndex:j+1];
                }
                
            }
        }
    }
    if ([self.rightNowProjectId isEqual:@10]) {
        
        for (NSMutableDictionary * deletDic in self.resultArray10.reverseObjectEnumerator) {
            NSMutableDictionary * dic = [deletDic objectForKey:@"master"];
            NSString * birthStr = [[dic objectForKey:@"Birthday"] substringWithRange:NSMakeRange(0, 10)];
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
            [dateFormatter setDateFormat:@"yyyy-MM-dd"];
            NSDate *birthDate = [dateFormatter dateFromString:birthStr];
            NSString * realAge = [self dateToOld:birthDate];
            if ([realAge integerValue]>8) {
                [self.resultArray10 removeObject:deletDic];
            }
            
        }
        
        for (int i = 0; i < self.resultArray10.count; ++i) {
            
            for (int j = 0; j < self.resultArray10.count-1; ++j) {
                NSMutableDictionary * dict = self.resultArray10[j];
                NSMutableDictionary * dic = [dict objectForKey:@"master"];
                NSString * birthStr = [[dic objectForKey:@"Birthday"] substringWithRange:NSMakeRange(0, 10)];
                NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
                [dateFormatter setDateFormat:@"yyyy-MM-dd"];
                NSDate *birthDate = [dateFormatter dateFromString:birthStr];
                NSString * realAge = [self dateToOld:birthDate];
                
                NSMutableDictionary * dict1 = self.resultArray10[j+1];
                NSMutableDictionary * dic1 = [dict1 objectForKey:@"master"];
                NSString * birthStr1 = [[dic1 objectForKey:@"Birthday"] substringWithRange:NSMakeRange(0, 10)];
                NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc]init];
                [dateFormatter1 setDateFormat:@"yyyy-MM-dd"];
                NSDate *birthDate1 = [dateFormatter dateFromString:birthStr1];
                NSString * realAge1 = [self dateToOld:birthDate1];
                //根据索引的`相邻两位`进行`比较`
                if ([realAge integerValue] > [realAge1 integerValue]) {
                    
                    [self.resultArray10 exchangeObjectAtIndex:j withObjectAtIndex:j+1];
                }
                
            }
        }
    }

    
    [self.rightTableView reloadData];
    [self changeUntestWithNum:[self.rightNowProjectId integerValue]];
//    [self changeTestAndUntestWithNum:[self.rightNowProjectId integerValue]];
    self.UntestBtn.selected = YES;
    self.testAllBtn.selected = NO;
}
//全部选择
-(void)pressTestAll{
    [self.resultArray1 removeAllObjects];
    [self.resultArray2 removeAllObjects];
    [self.resultArray4 removeAllObjects];
    [self.resultArray5 removeAllObjects];
    [self.resultArray6 removeAllObjects];
    [self.resultArray7 removeAllObjects];
    [self.resultArray8 removeAllObjects];
    [self.resultArray9 removeAllObjects];
    [self.resultArray10 removeAllObjects];
    
    NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
    
    NSMutableArray * testArray = [NSMutableArray arrayWithArray: [_allDataDictionary objectForKey:@"TestResult"]];//有结果的数据
    //本次营销计划选中的人(通过MASTER ID  判断)
    NSString * FS = [user objectForKey:@"finalSaleDic"];
    NSData * jsonData = [FS dataUsingEncoding:NSUTF8StringEncoding];
    NSMutableDictionary * mdic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
    NSMutableArray * masterIdArray = [mdic objectForKey:@"finalSaleDic"];
    
    for (NSNumber * iidd in masterIdArray) {//循环遍历已选中的孩子ID
        
        for (NSMutableDictionary * dic in testArray) {//循环遍历testResult
            NSMutableDictionary * masterdic = [dic objectForKey:@"master"];
            NSMutableArray * detailArr = [dic objectForKey:@"detailList"];
            
            if ([[masterdic objectForKey:@"Id"]isEqual:iidd]) {//查找选中孩子的MASTER和RESULT
                //            [self.resultArray addObject:dic];//将整个孩子的RESULT都加入数据源
                int a = 1;
                for (DetailListModel * mod in self.projectArray) {//总共九个项目，创建9组对象
                    
                    NSMutableDictionary * resultDic = [[NSMutableDictionary alloc]init];//对象字典（master.detailDic）
                    [resultDic setObject:masterdic forKey:@"master"];
                    for (NSMutableDictionary * dict in detailArr) {//遍历当前项目有数据的话DETAILDIC为数据。否则为空
                        if ([[dict objectForKey:@"ProjectId"]isEqual:mod.ProjectId]) {
                            [resultDic setObject:dict forKey:@"detailDic"];
                        }
                    }
                    if ([resultDic objectForKey:@"detailDic"] ==nil) {//否则为空
                        NSMutableDictionary * newDetai =[[NSMutableDictionary alloc]init];
                        [resultDic setObject:newDetai forKey:@"detailDic"];
                    }
                    //分别按ID 将对象加入数组源
                    if (a ==1) {
                        [self.resultArray1 addObject:resultDic];
                    }
                    if (a ==2) {
                        [self.resultArray2 addObject:resultDic];
                    }
                    if (a ==3) {
                        [self.resultArray4 addObject:resultDic];
                    }
                    if (a ==4) {
                        [self.resultArray5 addObject:resultDic];
                    }
                    if (a ==5) {
                        [self.resultArray6 addObject:resultDic];
                    }
                    if (a ==6) {
                        [self.resultArray7 addObject:resultDic];
                    }
                    if (a ==7) {
                        [self.resultArray8 addObject:resultDic];
                    }
                    if (a ==8) {
                        [self.resultArray9 addObject:resultDic];
                    }
                    if (a ==9) {
                        [self.resultArray10 addObject:resultDic];
                    }
                    a++;
                    
                }
            }
        }
    }
    
    //如果是20米和平衡木 对数据源根据年龄排序
    
    
    
    
    if ([self.rightNowProjectId isEqual:@4]) {
        
        for (int i = 0; i <self.resultArray4.count; ++i) {
            
            //遍历数组的每一个`索引`（不包括最后一个,因为比较的是j+1）
            for (int j = 0; j <self.resultArray4.count-1; ++j) {
                NSMutableDictionary * dict = self.resultArray4[j];
                NSMutableDictionary * dic = [dict objectForKey:@"master"];
                NSString * birthStr = [[dic objectForKey:@"Birthday"] substringWithRange:NSMakeRange(0, 10)];
                NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
                [dateFormatter setDateFormat:@"yyyy-MM-dd"];
                NSDate *birthDate = [dateFormatter dateFromString:birthStr];
                NSString * realAge = [self dateToOld:birthDate];
                
                NSMutableDictionary * dict1 = self.resultArray4[j+1];
                NSMutableDictionary * dic1 = [dict1 objectForKey:@"master"];
                NSString * birthStr1 = [[dic1 objectForKey:@"Birthday"] substringWithRange:NSMakeRange(0, 10)];
                NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc]init];
                [dateFormatter1 setDateFormat:@"yyyy-MM-dd"];
                NSDate *birthDate1 = [dateFormatter dateFromString:birthStr1];
                NSString * realAge1 = [self dateToOld:birthDate1];
                //根据索引的`相邻两位`进行`比较`
                if ([realAge integerValue] < [realAge1 integerValue]) {
                    
                    [self.resultArray4 exchangeObjectAtIndex:j withObjectAtIndex:j+1];
                }
                
            }
        }
    }
    if ([self.rightNowProjectId isEqual:@10]) {
        
        for (int i = 0; i < self.resultArray10.count; ++i) {
            
            for (int j = 0; j < self.resultArray10.count-1; ++j) {
                NSMutableDictionary * dict = self.resultArray10[j];
                NSMutableDictionary * dic = [dict objectForKey:@"master"];
                NSString * birthStr = [[dic objectForKey:@"Birthday"] substringWithRange:NSMakeRange(0, 10)];
                NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
                [dateFormatter setDateFormat:@"yyyy-MM-dd"];
                NSDate *birthDate = [dateFormatter dateFromString:birthStr];
                NSString * realAge = [self dateToOld:birthDate];
                
                NSMutableDictionary * dict1 = self.resultArray10[j+1];
                NSMutableDictionary * dic1 = [dict1 objectForKey:@"master"];
                NSString * birthStr1 = [[dic1 objectForKey:@"Birthday"] substringWithRange:NSMakeRange(0, 10)];
                NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc]init];
                [dateFormatter1 setDateFormat:@"yyyy-MM-dd"];
                NSDate *birthDate1 = [dateFormatter dateFromString:birthStr1];
                NSString * realAge1 = [self dateToOld:birthDate1];
                //根据索引的`相邻两位`进行`比较`
                if ([realAge integerValue] > [realAge1 integerValue]) {
                    
                    [self.resultArray10 exchangeObjectAtIndex:j withObjectAtIndex:j+1];
                }
                
            }
        }
    }
    

    [self.rightTableView reloadData];
    [self changeTestNum:[self.rightNowProjectId integerValue]];
//    [self changeTestAndUntestWithNum:[self.rightNowProjectId integerValue]];
    self.UntestBtn.selected = NO;
    self.testAllBtn.selected = YES;
}


//
//#pragma mark 数据测试是否完成的上传提示
//-(void)creatTestDone{
//     //蒙版
//    UIWindow * window = [UIApplication sharedApplication].keyWindow;
//    _backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
//    _backView.backgroundColor = [UIColor blackColor];
//    _backView.alpha = 0.52;
//    [window addSubview:_backView];
//    
//    self.baseAlertView = [[BaseAlertView alloc]init];
//    [window addSubview:self.baseAlertView];
//    [self.baseAlertView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerX.mas_equalTo(window.mas_centerX);
//        make.centerY.mas_equalTo(window.mas_centerY);
//        make.size.mas_equalTo(CGSizeMake(500*AutoScaleHorizontal, 250*AutoScaleVertical));
//    }];
//    self.baseAlertView.infomationLabel.text = @"测试已完成，是否现在上传？";
//    [self.baseAlertView.infomationLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(self.baseAlertView.greenView.mas_bottom).with.offset(50*AutoScaleVertical);
//        make.centerX.mas_equalTo(self.baseAlertView);
//        make.size.mas_equalTo(CGSizeMake(300*AutoScaleHorizontal, 20*AutoScaleVertical));
//    }];
//    
//    self.baseAlertView.infomationDetailLabel = [[UILabel alloc]init];
//    self.baseAlertView.infomationDetailLabel.numberOfLines = 0;
//    self.baseAlertView.infomationDetailLabel.text = @"提示:上传后会结束目前测试";
//    self.baseAlertView.infomationDetailLabel.font = [UIFont systemFontOfSize:18*AutoScaleHorizontal];
//    self.baseAlertView.infomationDetailLabel.textColor = [UIColor grayColor];
//    [self.baseAlertView.infomationDetailLabel setTextAlignment:NSTextAlignmentCenter];
//    [self.baseAlertView addSubview:self.baseAlertView.infomationDetailLabel];
//    [self.baseAlertView.infomationDetailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerX.mas_equalTo(self.baseAlertView.mas_centerX);
//        make.top.mas_equalTo(self.baseAlertView.infomationLabel.mas_bottom).with.offset(25*AutoScaleVertical);
//        make.size.mas_equalTo(CGSizeMake(300*AutoScaleHorizontal, 20*AutoScaleVertical));
//    }];
//
//    
//    //    self.alertView.remindLabel.text = @"提醒";
//    [self.baseAlertView.yesBtn addTarget:self action:@selector(pressAlertYes) forControlEvents:UIControlEventTouchUpInside];
//    [self.baseAlertView.noBtn addTarget:self action:@selector(pressAlertNo) forControlEvents:UIControlEventTouchUpInside];
//}
//
//-(void)pressAlertYes{
//    NSLog(@"确定删除");
//    [_backView removeFromSuperview];
//    [self.baseAlertView removeFromSuperview];
//}
//-(void)pressAlertNo{
//    NSLog(@"取消删除");
//    [_backView removeFromSuperview];
//    [self.baseAlertView removeFromSuperview];
//}
//
//
//////////////未完成VIEW上传
//-(void)creatTestUnDone{
//    
//    //蒙版
//    UIWindow * window = [UIApplication sharedApplication].keyWindow;
//    _backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
//    _backView.backgroundColor = [UIColor blackColor];
//    _backView.alpha = 0.52;
//    [window addSubview:_backView];
//    
//    self.baseAlertView = [[BaseAlertView alloc]init];
//    [window addSubview:self.baseAlertView];
//    [self.baseAlertView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerX.mas_equalTo(window.mas_centerX);
//        make.centerY.mas_equalTo(window.mas_centerY);
//        make.size.mas_equalTo(CGSizeMake(500*AutoScaleHorizontal, 250*AutoScaleVertical));
//    }];
//    self.baseAlertView.infomationLabel.text = @"测试数据未测完，确认上传？";
//    [self.baseAlertView.infomationLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(self.baseAlertView.greenView.mas_bottom).with.offset(50*AutoScaleVertical);
//        make.centerX.mas_equalTo(self.baseAlertView);
//        make.size.mas_equalTo(CGSizeMake(300*AutoScaleHorizontal, 20*AutoScaleVertical));
//    }];
//    
//    self.baseAlertView.infomationDetailLabel = [[UILabel alloc]init];
//    self.baseAlertView.infomationDetailLabel.numberOfLines = 0;
//    self.baseAlertView.infomationDetailLabel.text = @"提示:上传后会结束目前测试";
//    self.baseAlertView.infomationDetailLabel.font = [UIFont systemFontOfSize:18*AutoScaleHorizontal];
//    self.baseAlertView.infomationDetailLabel.textColor = [UIColor grayColor];
//    [self.baseAlertView.infomationDetailLabel setTextAlignment:NSTextAlignmentCenter];
//    [self.baseAlertView addSubview:self.baseAlertView.infomationDetailLabel];
//    [self.baseAlertView.infomationDetailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerX.mas_equalTo(self.baseAlertView.mas_centerX);
//        make.top.mas_equalTo(self.baseAlertView.infomationLabel.mas_bottom).with.offset(25*AutoScaleVertical);
//        make.size.mas_equalTo(CGSizeMake(300*AutoScaleHorizontal, 20*AutoScaleVertical));
//    }];
//    
//    
//    //    self.alertView.remindLabel.text = @"提醒";
//    [self.baseAlertView.yesBtn addTarget:self action:@selector(pressAlertYess) forControlEvents:UIControlEventTouchUpInside];
//    [self.baseAlertView.noBtn addTarget:self action:@selector(pressAlertNos) forControlEvents:UIControlEventTouchUpInside];
//}
//
//-(void)pressAlertYess{
//    NSLog(@"确定删除");
//    [_backView removeFromSuperview];
//    [self.baseAlertView removeFromSuperview];
//}
//-(void)pressAlertNos{
//    NSLog(@"取消删除");
//    [_backView removeFromSuperview];
//    [self.baseAlertView removeFromSuperview];
//}

#pragma mark 左右导航栏按钮方法
-(void)leftBarBtnClicked{
    NSLog(@"左按钮");
    [self.navigationController popViewControllerAnimated:YES];
}
//上传
-(void)rightBarBtnClicked{
    NSLog(@"右按钮");
    [self uploadData];
}
#pragma mark 添加孩子
-(void)rightBarBtnClicked2{
    NSLog(@"右按钮2");
    NSLog(@"右边");
    //蒙版
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    _backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _backView.backgroundColor = [UIColor blackColor];
    _backView.alpha = 0.52;
    [window addSubview:_backView];
    
    self.AddChildView = [[AddChildView alloc]init];
    [window addSubview:self.AddChildView];
    [self.AddChildView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(window.mas_centerX);
        make.centerY.mas_equalTo(window.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(600*AutoScaleHorizontal, 350*AutoScaleVertical));
    }];
    
    self.AddChildView.nameText.delegate  =self;
    self.AddChildView.PhoneText.delegate  =self;
    [self.AddChildView.cancelBtn addTarget:self action:@selector(pressCancel) forControlEvents:UIControlEventTouchUpInside];
    [self.AddChildView.realBirthBtn addTarget:self action:@selector(pressBirth) forControlEvents:UIControlEventTouchUpInside];
    [self.AddChildView.phoneLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.AddChildView.birthdayLabel.mas_bottom).with.offset(30*AutoScaleVertical);
        make.left.mas_equalTo(self.AddChildView.mas_left).with.offset(75*AutoScaleHorizontal);
        make.size.mas_equalTo(CGSizeMake(75*AutoScaleHorizontal, 20*AutoScaleVertical));
    }];
    
    [self.AddChildView.classLabel removeFromSuperview];
    [self.AddChildView.redStarLabel3 removeFromSuperview];
    
    [self.AddChildView.confirmBtn addTarget:self action:@selector(pressConfirmm) forControlEvents:UIControlEventTouchUpInside];
}
//取消添加
-(void)pressCancel{
    [_backView removeFromSuperview];
    [self.AddChildView removeFromSuperview];
}
//点击生日调取日历
-(void)pressBirth{
    [self.AddChildView.nameText resignFirstResponder];
    [self.AddChildView.PhoneText resignFirstResponder];
    _datePickerView = [[SDDatePickerView alloc]init];
    _datePickerView.delegate = self;
}
//确定添加孩子
-(void)pressConfirmm{
    NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
    
    if(self.AddChildView.nameText.text.length ==0){
        [self.AddChildView.nameText resignFirstResponder];
        [self.AddChildView.PhoneText resignFirstResponder];
        [self showMessage:@"孩子姓名不能为空"];
    }else if (self.AddChildView.realBirthLabel.text.length==0){
        [self.AddChildView.nameText resignFirstResponder];
        [self.AddChildView.PhoneText resignFirstResponder];
        [self showMessage:@"孩子生日不能为空"];
    }else{
        NSMutableArray * dataArr = [_allDataDictionary objectForKey:@"TestResult"];
        NSMutableDictionary * newDic = [[NSMutableDictionary alloc]init];
        NSMutableDictionary * masterDic = [[NSMutableDictionary alloc]init];
        //性别
        NSNumber * newGender = @0;
        if (self.AddChildView.boyBtn.selected ==YES) {
            newGender = @1;
        }else{
            newGender = @2;
        }
        
        //获取当前时间
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
        NSDate *datenow = [NSDate date];
        NSString *currentTimeString = [formatter stringFromDate:datenow];
        NSString * currentTime = [currentTimeString substringWithRange:NSMakeRange(0, 10)];
        
        //营销计划的ID
        NSMutableArray * proArr  = [[NSMutableArray alloc]init];
        proArr = [_allDataDictionary objectForKey:@"Programs"];//营销计划的数据 用type+id判断
        NSNumber* proid = [[NSNumber alloc]init];
        for (NSMutableDictionary * dic in proArr) {
            if ([[dic objectForKey:@"Type"]isEqual:@0]) {
                proid = [dic objectForKey:@"Id"]; //取得营销计划的ID
            }
        }
        //新ID
        NSInteger newbabyId = [[user objectForKey:@"CreatMasterId"]integerValue]-1;
        NSNumber * babyId = [NSNumber numberWithInt:newbabyId];
        [user removeObjectForKey:@"CreatMasterId"];
        [user setObject:babyId forKey:@"CreatMasterId"];
        
        [masterDic setObject:self.AddChildView.realBirthLabel.text forKey:@"Birthday"];
        NSString * birthStr = [self.AddChildView.realBirthLabel.text substringWithRange:NSMakeRange(0, 10)];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        NSDate *birthDate = [dateFormatter dateFromString:birthStr];
        NSString * realAge = [self dateToOld:birthDate];
        NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
        [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
        NSNumber *numrealAge = [numberFormatter numberFromString:realAge];
        if ([numrealAge integerValue] < 3) {
            [masterDic setObject:@3 forKey:@"Age"];
        }else{
        [masterDic setObject:numrealAge forKey:@"Age"];
        }
        [masterDic setObject:self.AddChildView.nameText.text forKey:@"ChildName"];
        [masterDic setObject:currentTime forKey:@"CreateTime"];
        [masterDic setObject:newGender forKey:@"Gender"];
        [masterDic setObject:self.AddChildView.PhoneText.text forKey:@"Phone"];
        [masterDic setObject:proid forKey:@"ProgramId"];
        [masterDic setObject:[user objectForKey:@"AgencyId"]  forKey:@"ServiceUnitId"];
        [masterDic setObject:babyId forKey:@"Id"];
        //给他一个空的项目数组
        NSMutableArray * detailNewArr = [[NSMutableArray alloc]init];
        
        [newDic setObject:masterDic forKey:@"master"];
        [newDic setObject:detailNewArr forKey:@"detailList"];
//        [dataArr addObject:newDic];
        [dataArr insertObject:newDic atIndex:0];
        //更新数据源
        
        [_allDataDictionary removeObjectForKey:@"TestResult"];
        [_allDataDictionary setObject:dataArr forKey:@"TestResult"];
        NSData * data = [NSJSONSerialization dataWithJSONObject:_allDataDictionary options:NSJSONWritingPrettyPrinted error:nil];
        NSString * allStr = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        [user removeObjectForKey:@"AllData"];
        [user setObject:allStr forKey:@"AllData"];
        
        //本次营销计划选中的人(通过MASTER ID  判断)
        if ([user objectForKey:@"finalSaleDic"]!=nil) {
            
        NSString * FS = [user objectForKey:@"finalSaleDic"];
        NSData * jsonData = [FS dataUsingEncoding:NSUTF8StringEncoding];
        NSMutableDictionary * mdic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
        NSMutableArray * masterIdArray = [mdic objectForKey:@"finalSaleDic"];
        [masterIdArray addObject:babyId];
        [mdic removeObjectForKey:@"finalSaleDic"];
        [mdic setObject:masterIdArray forKey:@"finalSaleDic"];
        
        NSData * dataDic = [NSJSONSerialization dataWithJSONObject:mdic options:NSJSONWritingPrettyPrinted error:nil];
        NSString * FSS = [[NSString alloc]initWithData:dataDic encoding:NSUTF8StringEncoding];
        //
        
        [user removeObjectForKey:@"finalSaleDic"];
        [user setObject:FSS forKey:@"finalSaleDic"];
        
        [user synchronize];
        }else{
            NSMutableDictionary * finalDic = [[NSMutableDictionary alloc]init];
            NSMutableArray * saleFinalArray = [[NSMutableArray alloc]init];//本地存储最终确定的计划测试人
            [saleFinalArray addObject:babyId];
            [finalDic setObject:saleFinalArray forKey:@"finalSaleDic"];//营销场景最终确定的人
            NSData * dataDic = [NSJSONSerialization dataWithJSONObject:finalDic options:NSJSONWritingPrettyPrinted error:nil];
            NSString * FS = [[NSString alloc]initWithData:dataDic encoding:NSUTF8StringEncoding];
            //
            [user removeObjectForKey:@"finalSaleDic"];
            [user setObject:FS forKey:@"finalSaleDic"];
            [user synchronize];
        }
        [_backView removeFromSuperview];
        [self.AddChildView removeFromSuperview];
//        [self.leftTableView removeFromSuperview];
//        [self.testAllBtn removeFromSuperview];
//        [self.UntestBtn removeFromSuperview];
//        [self.rightTableView removeFromSuperview];
        [self getData];
        [self showMessage:@"添加孩子成功"];
        [self changeTestNum:[self.rightNowProjectId integerValue]];
        
    }
    
    
    
    //    if ([user objectForKey:@"CreatMasterId"]==nil) {
    
  
}
// 日历代理方法
-(void)cancelBtnClick{
    NSLog(@"取消日历");
}
- (void)sureBtnWithDate:(NSString *)dateStr
{
    NSLog(@"选择日历");
    self.AddChildView.realBirthLabel.text = dateStr;
}

//text智能输入数字
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (textField == self.inputDataView.inputTextField || textField == self.AddChildView.PhoneText) {
        
    
    if ([self.rightNowProjectId isEqual:@4] || [self.rightNowProjectId isEqual:@5]||[self.rightNowProjectId isEqual:@7]) {
        return [self validateNumberWithDouHao:string];
    }else{
        return [self validateNumber:string];
    }
    }
    if (textField==self.AddChildView.nameText) {
            
            
            NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:ALPHANUM] invertedSet];
            NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
            return [string isEqualToString:filtered];
    }else{
        return YES;
    }
    
}
//可以输入逗号
- (BOOL)validateNumber:(NSString*)number {
    BOOL res = YES;
    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@".-0123456789"];
    int i = 0;
    while (i < number.length) {
        NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
        NSRange range = [string rangeOfCharacterFromSet:tmpSet];
        if (range.length == 0) {
            res = NO;
            break;
        }
        i++;
    }
    return res;
}
//不能输入逗号
- (BOOL)validateNumberWithDouHao:(NSString*)number {
    BOOL res = YES;
    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"-0123456789"];
    int i = 0;
    while (i < number.length) {
        NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
        NSRange range = [string rangeOfCharacterFromSet:tmpSet];
        if (range.length == 0) {
            res = NO;
            break;
        }
        i++;
    }
    return res;
}

#pragma mark ---- 根据键盘高度将当前视图向上滚动同样高度
///键盘显示事件
- (void)keyboardWillShoww:(NSNotification *)notification {
    //获取键盘高度，在不同设备上，以及中英文下是不同的
    CGFloat kbHeight = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
    
    //计算出键盘顶端到inputTextView panel底端的距离(加上自定义的缓冲距离INTERVAL_KEYBOARD)
    CGFloat offset = kbHeight;
    
    // 取得键盘的动画时间，这样可以在视图上移的时候更连贯
    double duration = [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
    //将视图上移计算好的偏移
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    if(offset > 0) {
        [UIView animateWithDuration:duration animations:^{
            window.frame = CGRectMake(0.0f, -85*AutoScaleVertical, window.frame.size.width, window.frame.size.height);
        }];
    }
}

#pragma mark ---- 当键盘消失后，视图需要恢复原状
///键盘消失事件
- (void)keyboardWillHidee:(NSNotification *)notify {
    // 键盘动画时间
    double duration = [[notify.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    //视图下沉恢复原状
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    [UIView animateWithDuration:duration animations:^{
        window.frame = CGRectMake(0, 0, window.frame.size.width, window.frame.size.height);
    }];
}

//TEXTfield有数据时  按钮不可点击
-(void)change:(NSNotification *)notification
{
    if (self.inputDataView.inputTextField.text.length > 0) {
        self.inputDataView.unTestableBtn.userInteractionEnabled = NO;
        self.inputDataView.unTestableLabel.textColor = UIColorFromRGB(167, 167, 167);
        self.inputDataView.unTestableBtn.alpha = 0.3;
        
    }else
    {
        self.inputDataView.unTestableBtn.userInteractionEnabled = YES;
        self.inputDataView.unTestableLabel.textColor = [UIColor blackColor];
        self.inputDataView.unTestableBtn.alpha = 1;
    }
    
}


// 释放
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
    
}
#pragma mark 同步数据
-(void)uploadData{
    
    NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
    //拿到本地数据
    NSMutableArray * localChildListArray = [_allDataDictionary objectForKey:@"ChildList"];
    NSMutableArray * localClassListArray = [_allDataDictionary objectForKey:@"ClassList"];
    NSMutableArray * localProgramsArray = [_allDataDictionary objectForKey:@"Programs"];
    NSMutableArray * localTestResultArray = [_allDataDictionary objectForKey:@"TestResult"];
    //遍历得到需要上传的数据
    //*****************************agencyid////////////////////////////////////////////////////////////////////
    NSInteger  agencyId = [[user objectForKey:@"AgencyId"]integerValue];
    //agencyid////////////////////////////////////////////////////////////////////
    
    //*****************************删除的对象////////////////////////////////////////////////////////////////////
    NSMutableDictionary * deletedDic = [[NSMutableDictionary alloc]init];
    //已经删除的孩子数组
    NSMutableArray * ChildIdArr = [[NSMutableArray alloc]init];
    if ([user objectForKey:@"deleteChildArray"] != nil) {
        
        NSString* str = [user objectForKey:@"deleteChildArray"];
        NSData *jsonData = [str dataUsingEncoding:NSUTF8StringEncoding];
        
        NSMutableDictionary * dict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
        ChildIdArr = [dict objectForKey:@"deleteChildArray"];
    }
    //已经删除的班级数组
    NSMutableArray * ClassArr = [[NSMutableArray alloc]init];
    if ([user objectForKey:@"deleteClassArray"] != nil) {
        
        NSString* str1 = [user objectForKey:@"deleteClassArray"];
        NSData *jsonData1 = [str1 dataUsingEncoding:NSUTF8StringEncoding];
        NSMutableDictionary * dict1 = [NSJSONSerialization JSONObjectWithData:jsonData1 options:NSJSONReadingMutableContainers error:nil];
        ClassArr = [dict1 objectForKey:@"deleteClassArray"];
    }
    //已经删除的测试计划数组
    NSMutableArray * PlanArr = [[NSMutableArray alloc]init];
    if ([user objectForKey:@"deletePlanArray"] != nil) {
        
        NSString* str1 = [user objectForKey:@"deletePlanArray"];
        NSData *jsonData1 = [str1 dataUsingEncoding:NSUTF8StringEncoding];
        NSMutableDictionary * dict1 = [NSJSONSerialization JSONObjectWithData:jsonData1 options:NSJSONReadingMutableContainers error:nil];
        PlanArr = [dict1 objectForKey:@"deletePlanArray"];
    }
    //已经删除的营销孩子数组
    NSMutableArray * MasterArr = [[NSMutableArray alloc]init];
    if ([user objectForKey:@"deleteMasterArray"] != nil) {
        
        NSString* str1 = [user objectForKey:@"deleteMasterArray"];
        NSData *jsonData1 = [str1 dataUsingEncoding:NSUTF8StringEncoding];
        NSMutableDictionary * dict1 = [NSJSONSerialization JSONObjectWithData:jsonData1 options:NSJSONReadingMutableContainers error:nil];
        MasterArr = [dict1 objectForKey:@"deleteMasterArray"];
    }
    
    //将这四个数组设置成对象
    [deletedDic setObject:PlanArr forKey:@"ProgramIdArr"];
    [deletedDic setObject:MasterArr forKey:@"MasterIdArr"];
    [deletedDic setObject:ChildIdArr forKey:@"ChildIdArr"];
    [deletedDic setObject:ClassArr forKey:@"ClassArr"];
    //删除的对象////////////////////////////////////////////////////////////////////
    
    //***************************待上传的孩子childlist////////////////////////////////////////////////////////////////////
    NSMutableArray * readyToLoadChildListArray = [[NSMutableArray alloc]init];//childlist
    for (NSMutableDictionary * dict in localChildListArray) {
        NSInteger dictid =[[dict objectForKey:@"Id"]integerValue];
        if (dictid<0 || [dict objectForKey:@"1900V"] !=nil) {
            [readyToLoadChildListArray addObject:dict];
        }
    }
    //待上传的孩子childlist////////////////////////////////////////////////////////////////////
    
    //***************************待上传的班级classlist////////////////////////////////////////////////////////////////////
    NSMutableArray * readyToLoadClassListArray = [[NSMutableArray alloc]init];//classlist
    for (NSMutableDictionary * dict in localClassListArray) {
        NSInteger dictid =[[dict objectForKey:@"Id"]integerValue];
        if (dictid<0 || [dict objectForKey:@"1900V"] !=nil) {
            [readyToLoadClassListArray addObject:dict];
        }
    }
    //待上传的班级classlist////////////////////////////////////////////////////////////////////
    
    //***************************待上传的测试计划programlist////////////////////////////////////////////////////////////////////
    NSMutableArray * readyToLoadProgramsArray = [[NSMutableArray alloc]init];//program
    for (NSMutableDictionary * dict in localProgramsArray) {
        NSInteger dictid =[[dict objectForKey:@"Id"]integerValue];
        if (dictid<0 || [dict objectForKey:@"1900V"] !=nil) {
            [readyToLoadProgramsArray addObject:dict];
        }
    }
    //待上传的测试计划programlist////////////////////////////////////////////////////////////////////
    
    //***************************待上传的测试结果result////////////////////////////////////////////////////////////////////
    NSMutableArray * readyToLoadTestResultArray = [[NSMutableArray alloc]init];//masterDetail
    for (NSMutableDictionary * dict in localTestResultArray) {
        NSMutableDictionary * masterdic = [dict objectForKey:@"master"];
        NSMutableArray * detailArr = [dict objectForKey:@"detailList"];
        NSMutableDictionary * newDICTIONARY = [[NSMutableDictionary alloc]init];
        int a = 0;
        //先判断MASTER
        NSInteger dictid =[[masterdic objectForKey:@"Id"]integerValue];
        if (dictid<0 || [masterdic objectForKey:@"1900V"] !=nil) {
            a+=1;
        }else{//master没有更新的话， 判断与之对应的detail
            for (NSMutableDictionary * detailDic in detailArr) {
                NSInteger dictid =[[detailDic objectForKey:@"Id"]integerValue];
                if (dictid<0 || [detailDic objectForKey:@"1900V"] !=nil) {
                    a+=1;
                }
            }
        }
        if (a!=0) {
            [newDICTIONARY setObject:masterdic forKey:@"ResultMaster"];
            [newDICTIONARY setObject:detailArr forKey:@"ResultDetail"];
            [readyToLoadTestResultArray addObject:newDICTIONARY];
        }
    }
    if (readyToLoadTestResultArray.count ==0) {
        //没有任何修改  给空数组和空字典
        NSMutableDictionary * dic1= [[NSMutableDictionary alloc]init];
        NSMutableArray * arr1 = [[NSMutableArray alloc]init];
        NSMutableDictionary * newDICTIONARY = [[NSMutableDictionary alloc]init];
        [newDICTIONARY setObject:dic1 forKey:@"ResultMaster"];
        [newDICTIONARY setObject:arr1 forKey:@"ResultDetail"];
        [readyToLoadTestResultArray addObject:newDICTIONARY];
    }
    //待上传的结果result////////////////////////////////////////////////////////////////////
    
    
    //**********************************************************************************************************************************//
    //**********************************************************************************************************************************//
    //**********************************************************************************************************************************//
    //开始上传
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    //    AFNetworkReachabilityStatusUnknown          = -1,
    //    AFNetworkReachabilityStatusNotReachable     = 0,
    //    AFNetworkReachabilityStatusReachableViaWWAN = 1,
    //    AFNetworkReachabilityStatusReachableViaWiFi = 2,
    if (manager.networkReachabilityStatus == AFNetworkReachabilityStatusUnknown || manager.networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) {
        [self showMessage:@"当前网络状态不佳，请检验"];
    }else{
        /////////////////////请求
        //菊花
        self.HUD = [[MBProgressHUD alloc]initWithView:self.view];
        //    self.HUD.mode = MBProgressHUDModeIndeterminate;
        //     self.HUD.opacity = 1;
        self.HUD.color = [UIColor whiteColor];
        self.HUD.dimBackground = YES;
        //设置菊花颜色  只能设置菊花的颜色
        self.HUD.activityIndicatorColor = [UIColor redColor];
        [self.view addSubview:self.HUD];
        [self.HUD showAnimated:YES];
        NSString * urls = @"/upload/testresult";
        NSString * url = [NSString stringWithFormat:@"%@%@",URL,urls];
        NSLog(@"%@",url);
        //        NSString * kuohao = @"[]";
        NSNumber * aId= [NSNumber numberWithInteger:agencyId];
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/html",@"application/json", nil];
        //设置参数
        NSData * datachild = [NSJSONSerialization dataWithJSONObject:readyToLoadChildListArray options:0  error:nil];
        NSString * jsStrchild = [[NSString alloc]initWithData:datachild encoding:NSUTF8StringEncoding];//将所需数据转成JS字符串
        //            NSString *  jsStrchild1 = [self stringCutN:jsStr];
        
        NSData * dataclass = [NSJSONSerialization dataWithJSONObject:readyToLoadClassListArray options:0  error:nil];
        NSString * jsStrclass = [[NSString alloc]initWithData:dataclass encoding:NSUTF8StringEncoding];//将所需数据转成JS字符串
        //            NSString *  jsStrclass1 = [self stringCutN:jsStr];
        
        NSData * datapro = [NSJSONSerialization dataWithJSONObject:readyToLoadProgramsArray options:0  error:nil];
        NSString * jsStrpro = [[NSString alloc]initWithData:datapro encoding:NSUTF8StringEncoding];//将所需数据转成JS字符串
        //            NSString *  jsStrpro1 = [self stringCutN:jsStr];
        
        NSData * dataresult = [NSJSONSerialization dataWithJSONObject:readyToLoadTestResultArray options:0  error:nil];
        NSString * jsStrresult = [[NSString alloc]initWithData:dataresult encoding:NSUTF8StringEncoding];//将所需数据转成JS字符串
        //        NSString *  jsStrresult1 = [self stringCutN:jsStr];
        
        NSData *jsonDataPrama = [NSJSONSerialization dataWithJSONObject:deletedDic options:NSJSONWritingPrettyPrinted error:nil];
        NSString *jsonStringDic;
        jsonStringDic = [[NSString alloc]initWithData:jsonDataPrama encoding:NSUTF8StringEncoding];
        NSString *  useFulString1 = [self stringCutN:jsonStringDic];
        
        NSDictionary * parama = @{@"ServiceUnitId":aId,@"JsonChildList":jsStrchild,@"JsonClassList":jsStrclass,@"JsonProgramList":jsStrpro,@"JsonTestResult":jsStrresult,@"JsonDeleteObj":useFulString1};
        
        //            NSData * Pramadata = [NSJSONSerialization dataWithJSONObject:parama options:NSJSONWritingPrettyPrinted error:nil];
        //            NSString * pramaDic;
        //            pramaDic = [[NSString alloc]initWithData:Pramadata encoding:NSUTF8StringEncoding];
        //        NSString *  prama1 = [self stringCutN:pramaDic];
        ////
        //
        manager.requestSerializer.timeoutInterval = 30;
        [manager POST:url parameters:parama progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSLog(@"上传大数据 success");
            NSMutableDictionary *relationDict=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            NSMutableDictionary * newoldDic =[relationDict objectForKey:@"DeleteIdRelation"];//新旧关系
            
            NSString * str = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
            NSLog(@"responseObject数据转成了json类型的数据 ＝ %@",str);
            [user removeObjectForKey:@"deleteChildArray"];
            [user removeObjectForKey:@"deleteClassArray"];
            [user removeObjectForKey:@"deletePlanArray"];
            [user removeObjectForKey:@"deleteMasterArray"];
            /**********************************************************************************************************************************/
            //清除新数据的负数ID 和 1900V 改变字段
            //***************************待上传的孩子childlist////////////////////////////////////////////////////////////////////
            NSMutableArray * childNewOld = [newoldDic objectForKey:@"ChildIdRelation"];
            for (NSMutableDictionary * dict in localChildListArray.reverseObjectEnumerator) {
                if ( [dict objectForKey:@"1900V"] !=nil) {
                    [dict removeObjectForKey:@"1900V"];
                }
                for (NSMutableDictionary * childNewoldDic in childNewOld) {
                    if ([[dict objectForKey:@"Id"]isEqual:[childNewoldDic objectForKey:@"OldId"]] ) {
                        [dict removeObjectForKey:@"Id"];
                        [dict setObject:[childNewoldDic objectForKey:@"NewId"] forKey:@"Id"];
                    }
                }
            }
            //待上传的孩子childlist////////////////////////////////////////////////////////////////////
            
            //***************************待上传的班级classlist////////////////////////////////////////////////////////////////////
            NSMutableArray * classNewOld = [newoldDic objectForKey:@"ClassIdRelation"];
            for (NSMutableDictionary * dict in localClassListArray.reverseObjectEnumerator) {
                if ([dict objectForKey:@"1900V"] !=nil) {
                    [dict removeObjectForKey:@"1900V"];
                }
                for (NSMutableDictionary * classNewoldDic in classNewOld) {
                    if ([[dict objectForKey:@"Id"]isEqual:[classNewoldDic objectForKey:@"OldId"]] ) {
                        [dict removeObjectForKey:@"Id"];
                        [dict setObject:[classNewoldDic objectForKey:@"NewId"] forKey:@"Id"];
                    }
                }
            }
            //待上传的班级classlist////////////////////////////////////////////////////////////////////
            
            //***************************待上传的测试计划programlist////////////////////////////////////////////////////////////////////
            NSMutableArray * programNewOld = [newoldDic objectForKey:@"ProgramIdRelation"];
            for (NSMutableDictionary * dict in localProgramsArray.reverseObjectEnumerator) {
                if ([dict objectForKey:@"1900V"] !=nil) {
                    [dict removeObjectForKey:@"1900V"];
                }
                for (NSMutableDictionary * programNewoldDic in programNewOld) {
                    if ([[dict objectForKey:@"Id"]isEqual:[programNewoldDic objectForKey:@"OldId"]] ) {
                        [dict removeObjectForKey:@"Id"];
                        [dict setObject:[programNewoldDic objectForKey:@"NewId"] forKey:@"Id"];
                    }
                    
                }
            }
            //待上传的测试计划programlist////////////////////////////////////////////////////////////////////
            
            //***************************待上传的测试结果result////////////////////////////////////////////////////////////////////
            NSMutableArray * masterNewOld = [newoldDic objectForKey:@"MasterIdRelation"];
            NSMutableArray * detailNewOld = [newoldDic objectForKey:@"DetailIdRelation"];
            for (NSMutableDictionary * dict in localTestResultArray.reverseObjectEnumerator) {
                NSMutableDictionary * masterdic = [dict objectForKey:@"master"];
                NSMutableArray * detailArr = [dict objectForKey:@"detailList"];
                //先判断MASTER
                if ([masterdic objectForKey:@"1900V"] !=nil) {
                    [masterdic removeObjectForKey:@"1900V"];
                }
                for (NSMutableDictionary * masterNewoldDic in masterNewOld) {
                    if ([[masterdic objectForKey:@"Id"]isEqual:[masterNewoldDic objectForKey:@"OldId"]] ) {
                        [masterdic removeObjectForKey:@"Id"];
                        [masterdic setObject:[masterNewoldDic objectForKey:@"NewId"] forKey:@"Id"];
                    }
                }
                //master没有更新的话， 判断与之对应的detail
                for (NSMutableDictionary * detailDic in detailArr.reverseObjectEnumerator) {
                    if ([detailDic objectForKey:@"1900V"] !=nil) {
                        [detailDic removeObjectForKey:@"1900V"];
                    }
                    for (NSMutableDictionary * detailNewoldDic in detailNewOld) {
                        if ([[detailDic objectForKey:@"Id"]isEqual:[detailNewoldDic objectForKey:@"OldId"]] ) {
                            [detailDic removeObjectForKey:@"Id"];
                            [detailDic setObject:[detailNewoldDic objectForKey:@"NewId"] forKey:@"Id"];
                        }
                    }
                    
                }
                
            }
            //待上传的结果result////////////////////////////////////////////////////////////////////
            /**********************************************************************************************************************************/
            
            //将新孩子的负数ID替换? 营销场景
            if ([user objectForKey:@"finalSaleDic"]!=nil) {
                
                NSString * FS = [user objectForKey:@"finalSaleDic"];
                NSData * jsonData = [FS dataUsingEncoding:NSUTF8StringEncoding];
                NSMutableDictionary * mdic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
                NSMutableArray * masterIdArray = [mdic objectForKey:@"finalSaleDic"];
                NSMutableDictionary * newIDDic = [relationDict objectForKey:@"DeleteIdRelation"];//全部关系
                NSMutableArray * newIdArray = [newIDDic objectForKey:@"MasterIdRelation"];//MASTER关系
                
                //循环遍历 替换负数ID
                for ( NSNumber * oldId in masterIdArray.reverseObjectEnumerator) {
                    for (NSMutableDictionary * dic in newIdArray) {
                        if ([oldId isEqual:[dic objectForKey:@"OldId"]]) {
                            [masterIdArray removeObject:oldId];
                            [masterIdArray addObject:[dic objectForKey:@"NewId"]];
                        }
                    }
                }
                
                [mdic removeObjectForKey:@"finalSaleDic"];
                [mdic setObject:masterIdArray forKey:@"finalSaleDic"];
                
                NSData * dataDic = [NSJSONSerialization dataWithJSONObject:mdic options:NSJSONWritingPrettyPrinted error:nil];
                NSString * FSS = [[NSString alloc]initWithData:dataDic encoding:NSUTF8StringEncoding];
                //
                
                [user removeObjectForKey:@"finalSaleDic"];
                [user setObject:FSS forKey:@"finalSaleDic"];
                
                [user synchronize];
            }
            [self reloadAllData];
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"上传大数据 error");
            NSError * err = error;
            [self showMessage:@"同步失败，请稍后重试"];
            NSLog(@"登陆因为这个失败了%@",err.description);
            [self.HUD removeFromSuperview];
            self.HUD = nil;
        }];
    }
    
}

#pragma mark
#pragma mark 同步数据
-(void)reloadAllData{
    NSLog(@"开始同步");
    //检测网络
    AFNetworkReachabilityManager * manager = [AFNetworkReachabilityManager sharedManager];
    //    AFNetworkReachabilityStatusUnknown          = -1,
    //    AFNetworkReachabilityStatusNotReachable     = 0,
    //    AFNetworkReachabilityStatusReachableViaWWAN = 1,
    //    AFNetworkReachabilityStatusReachableViaWiFi = 2,
    if (manager.networkReachabilityStatus == AFNetworkReachabilityStatusUnknown || manager.networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) {
        [self showMessage:@"当前网络状态不佳，请检验"];
    }else{
        NSString * urls = @"/upload/testresult";
        NSString * url = [NSString stringWithFormat:@"%@%@",URL,urls];
        NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
        NSString * kuohao =@"[]";
        NSDictionary *parama = @{@"ServiceUnitId":[user objectForKey:@"AgencyId"],@"ChildList":kuohao,@"ClassList":kuohao,@"ProgramList":kuohao};
        [[HttpRequest sharedInstance] postWithURLString:url parameters:parama success:^(id responseObject) {
            NSLog(@"更新成功");
            
            [self.HUD removeFromSuperview];
            self.HUD = nil;
//            NSString * str = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
//            NSLog(@"responseObject数据转成了json类型的数据 ＝ %@",str);
            NSMutableDictionary *dict=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            //将未分班放在最后
            NSMutableArray * classArray=[NSMutableArray array];
            classArray = [dict objectForKey:@"ClassList"];
            NSDictionary * unClassed = [[NSDictionary alloc]init];
            for (NSDictionary*dic in classArray.reverseObjectEnumerator) {
                if ([[dic objectForKey:@"Name"]isEqualToString:@"未分班"]) {
                    unClassed = dic;
                    [classArray removeObject:dic];
                    
                }
            }
            //        [classArray insertObject:unClassed atIndex:0];
            [classArray addObject:unClassed];
            [dict removeObjectForKey:@"ClassList"];
            [dict setObject:classArray forKey:@"ClassList"];
            NSData * data = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
            NSString * allstr = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
            //
            [user removeObjectForKey:@"AllData"];
            [user setObject:allstr forKey:@"AllData"];
            [user synchronize];
            [self showMessage:@"同步成功"];
            [self getData];
           
        } failure:^(NSError *error) {
            [self showMessage:@"同步失败，请稍后再试"];
            [self.HUD removeFromSuperview];
            self.HUD = nil;
        }];
        
    }
}

//更改全部未测数字
-(void)changeTestNum:(NSInteger)num{
    
    switch (num) {
        case 1:
        {
            NSInteger count =  self.resultArray1.count;
            [self.testAllBtn setTitle:@"" forState:UIControlStateNormal];
            [self.testAllBtn setTitle:[NSString stringWithFormat:@"全部  %d",count ] forState:UIControlStateNormal];
            
            
        }
            break;
        case 2:
        {
            NSInteger count =  self.resultArray2.count;
            [self.testAllBtn setTitle:@"" forState:UIControlStateNormal];
            [self.testAllBtn setTitle:[NSString stringWithFormat:@"全部  %d",count ] forState:UIControlStateNormal];
            
            
        }
            
            break;
        case 4:
        {
            NSInteger count =  self.resultArray4.count;
            [self.testAllBtn setTitle:@"" forState:UIControlStateNormal];
            [self.testAllBtn setTitle:[NSString stringWithFormat:@"全部  %d",count ] forState:UIControlStateNormal];
            
        }
            
            break;
        case 5:
        {
            NSInteger count =  self.resultArray5.count;
            [self.testAllBtn setTitle:@"" forState:UIControlStateNormal];
            [self.testAllBtn setTitle:[NSString stringWithFormat:@"全部  %d",count ] forState:UIControlStateNormal];
            
            
        }
            
            break;
        case 6:
        {
            NSInteger count =  self.resultArray6.count;
            [self.testAllBtn setTitle:@"" forState:UIControlStateNormal];
            [self.testAllBtn setTitle:[NSString stringWithFormat:@"全部  %d",count ] forState:UIControlStateNormal];
            
            
        }
            
            break;
        case 7:
        {
            NSInteger count =  self.resultArray7.count;
            [self.testAllBtn setTitle:@"" forState:UIControlStateNormal];
            [self.testAllBtn setTitle:[NSString stringWithFormat:@"全部  %d",count ] forState:UIControlStateNormal];
            
            
        }
            
            break;
        case 8:
        {
            NSInteger count =  self.resultArray8.count;
            [self.testAllBtn setTitle:@"" forState:UIControlStateNormal];
            [self.testAllBtn setTitle:[NSString stringWithFormat:@"全部  %d",count ] forState:UIControlStateNormal];
            
            
        }
            
            break;
        case 9:
        {
            NSInteger count =  self.resultArray9.count;
            [self.testAllBtn setTitle:@"" forState:UIControlStateNormal];
            [self.testAllBtn setTitle:[NSString stringWithFormat:@"全部  %d",count ] forState:UIControlStateNormal];
            
            
        }
            
            break;
        case 10:
        {
            NSInteger count =  self.resultArray10.count;
            [self.testAllBtn setTitle:@"" forState:UIControlStateNormal];
            [self.testAllBtn setTitle:[NSString stringWithFormat:@"全部  %d",count ] forState:UIControlStateNormal];
            
            
        }
            
            break;
        default:
            break;
    }
    
    
    
    
}

//更改全部未测数字
-(void)changeUntestWithNum:(NSInteger)num{
    
    switch (num) {
        case 1:
        {
            
            
            NSInteger count =  self.resultArray1.count;
            NSInteger x = 0;
            for (NSMutableDictionary * dict in self.resultArray1.reverseObjectEnumerator) {
                NSMutableDictionary * dic = [dict objectForKey:@"detailDic"];
                if ([dic objectForKey:@"ProjectId"]!=nil) {
                    x+=1;
                }
            }
            [self.UntestBtn setTitle:@"" forState:UIControlStateNormal];
            [self.UntestBtn setTitle:[NSString stringWithFormat:@"未测  %d",count-x ] forState:UIControlStateNormal];
            
        }
            break;
        case 2:
        {
            NSInteger count =  self.resultArray2.count;
            NSInteger x = 0;
            for (NSMutableDictionary * dict in self.resultArray2.reverseObjectEnumerator) {
                NSMutableDictionary * dic = [dict objectForKey:@"detailDic"];
                if ([dic objectForKey:@"ProjectId"]!=nil) {
                    x+=1;
                }
            }
            [self.UntestBtn setTitle:@"" forState:UIControlStateNormal];
            [self.UntestBtn setTitle:[NSString stringWithFormat:@"未测  %d",count-x ] forState:UIControlStateNormal];
            
        }
            
            break;
        case 4:
        {
            NSInteger x = 0;
            for (NSMutableDictionary * deletDic in self.resultArray4.reverseObjectEnumerator) {
                NSMutableDictionary * dic = [deletDic objectForKey:@"master"];
                NSString * birthStr = [[dic objectForKey:@"Birthday"] substringWithRange:NSMakeRange(0, 10)];
                NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
                [dateFormatter setDateFormat:@"yyyy-MM-dd"];
                NSDate *birthDate = [dateFormatter dateFromString:birthStr];
                NSString * realAge = [self dateToOld:birthDate];
                if ([realAge integerValue]<=8) {
                    x+=1;
                }
            }
            NSInteger count =  self.resultArray4.count;
            
            for (NSMutableDictionary * dict in self.resultArray4.reverseObjectEnumerator) {
                NSMutableDictionary * dic = [dict objectForKey:@"detailDic"];
                if ([dic objectForKey:@"ProjectId"]!=nil) {
                    x+=1;
                }
            }
            [self.UntestBtn setTitle:@"" forState:UIControlStateNormal];
            [self.UntestBtn setTitle:[NSString stringWithFormat:@"未测  %d",count-x ] forState:UIControlStateNormal];
            
        }
            
            break;
        case 5:
        {
            NSInteger count =  self.resultArray5.count;
            NSInteger x = 0;
            for (NSMutableDictionary * dict in self.resultArray5.reverseObjectEnumerator) {
                NSMutableDictionary * dic = [dict objectForKey:@"detailDic"];
                if ([dic objectForKey:@"ProjectId"]!=nil) {
                    x+=1;
                }
            }
            [self.UntestBtn setTitle:@"" forState:UIControlStateNormal];
            [self.UntestBtn setTitle:[NSString stringWithFormat:@"未测  %d",count-x ] forState:UIControlStateNormal];
            
        }
            
            break;
        case 6:
        {
            NSInteger count =  self.resultArray6.count;
            NSInteger x = 0;
            for (NSMutableDictionary * dict in self.resultArray6.reverseObjectEnumerator) {
                NSMutableDictionary * dic = [dict objectForKey:@"detailDic"];
                if ([dic objectForKey:@"ProjectId"]!=nil) {
                    x+=1;
                }
            }
            [self.UntestBtn setTitle:@"" forState:UIControlStateNormal];
            [self.UntestBtn setTitle:[NSString stringWithFormat:@"未测  %d",count-x ] forState:UIControlStateNormal];
            
        }
            
            break;
        case 7:
        {
            NSInteger count =  self.resultArray7.count;
            
            NSInteger x = 0;
            for (NSMutableDictionary * dict in self.resultArray7.reverseObjectEnumerator) {
                NSMutableDictionary * dic = [dict objectForKey:@"detailDic"];
                if ([dic objectForKey:@"ProjectId"]!=nil || [[dic objectForKey:@"Result"]isEqual:@-111]) {
                    x+=1;
                }
            }
            [self.UntestBtn setTitle:@"" forState:UIControlStateNormal];
            [self.UntestBtn setTitle:[NSString stringWithFormat:@"未测  %d",count-x ] forState:UIControlStateNormal];
            
        }
            
            break;
        case 8:
        {
            NSInteger count =  self.resultArray8.count;
            
            NSInteger x = 0;
            for (NSMutableDictionary * dict in self.resultArray8.reverseObjectEnumerator) {
                NSMutableDictionary * dic = [dict objectForKey:@"detailDic"];
                if ([dic objectForKey:@"ProjectId"]!=nil && ![[dic objectForKey:@"Result"]isEqual:@-111]) {
                    x+=1;
                }
            }
            [self.UntestBtn setTitle:@"" forState:UIControlStateNormal];
            [self.UntestBtn setTitle:[NSString stringWithFormat:@"未测  %d",count-x ] forState:UIControlStateNormal];
            
        }
            
            break;
        case 9:
        {
            NSInteger count =  self.resultArray9.count;
            NSInteger x = 0;
            for (NSMutableDictionary * dict in self.resultArray9.reverseObjectEnumerator) {
                NSMutableDictionary * dic = [dict objectForKey:@"detailDic"];
                if ([dic objectForKey:@"ProjectId"]!=nil) {
                    x+=1;
                }
            }
            [self.UntestBtn setTitle:@"" forState:UIControlStateNormal];
            [self.UntestBtn setTitle:[NSString stringWithFormat:@"未测  %d",count-x ] forState:UIControlStateNormal];
            
        }
            
            break;
        case 10:
        {
            NSInteger x = 0;
            for (NSMutableDictionary * deletDic in self.resultArray10.reverseObjectEnumerator) {
                NSMutableDictionary * dic = [deletDic objectForKey:@"master"];
                NSString * birthStr = [[dic objectForKey:@"Birthday"] substringWithRange:NSMakeRange(0, 10)];
                NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
                [dateFormatter setDateFormat:@"yyyy-MM-dd"];
                NSDate *birthDate = [dateFormatter dateFromString:birthStr];
                NSString * realAge = [self dateToOld:birthDate];
                if ([realAge integerValue]>8) {
                    x+=1;
                }
            }
            NSInteger count =  self.resultArray10.count;
            
            for (NSMutableDictionary * dict in self.resultArray10.reverseObjectEnumerator) {
                NSMutableDictionary * dic = [dict objectForKey:@"detailDic"];
                if ([dic objectForKey:@"ProjectId"]!=nil) {
                    x+=1;
                }
            }
            [self.UntestBtn setTitle:@"" forState:UIControlStateNormal];
            [self.UntestBtn setTitle:[NSString stringWithFormat:@"未测  %d",count-x ] forState:UIControlStateNormal];
            
        }
            
            break;
        default:
            break;
    }
    
    
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
